Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 23, 2021 04:46 pm GMT

ReasonML & Rescript in 5 minutes

What is this language about?

It's a functional language that compiles to JavaScript (or to OCaml).

What is ML in ReasonML?

Nowadays ML usually stands for Machine Learning, but in the old days there was a programming language called "Meta Language" which is the ancestor of ReasonML, or Reason Meta Language.

Why rename?

ReasonML is compatible with both JS and OCaml, but lately it was mostly used in the JS ecosystem.
Rescript takes JS-related things from Reason and stops being limited by OCaml support.

What are the good things?

Immutability

All references are actually constants. Shadowing is widely used. Standard library functions are non-mutative and produce new instances if any changes are made. There is a workaround to create a mutable reference if needed but deliberate enough to be discouraging.

Piping and currying

Rescript is a functional language with no methods on objects, but you can call and chain functions in a familiar way with piping and currying, like
myList->List.length
or
myArray->Array.map(item => item * 2)

Sound type system and type inference

This is probably the main reason to choose rescript. It has strong type system and powerful type inference, so you are rarely required to explicitly define types, but the compiler always knows what the types are.

No folder requirements and no imports

This is a case in many languages, but coming from Java I really appreciate this feature. And Javascript developers love the no imports thing!

Full support of algebraic data types

It's very easy to describe any domain with the custom types
You can create type aliases like type eventId = string or complex types like

type event = {  id: eventId,  name: string,  uniqueName: option<string>,  description: string,  properties: list<property>,  types: list<eventType>}

The main construction in the language is the exhaustive switch optimized for pattern matching

It's accompanied by an empowered kind of enum called variants. There are options of variants with and without duck typing. Better to see it in action

Simple syntax

It's possible to start writing code after just a few hours of learning if you already know another programming language.

Relatively safe refactoring

The combination of a rigid type system and exhaustive switches make the compiler very efficient in finding bugs in the compile time.

What are the not so good things?

Simple syntax means it's verbose

There is not much syntax sugar, for example to unwrap an optional constant you'd have to write maybeSomething->Option.map(something -> something->performOperation) instead of maybeSomething?.performOperation() in some other languages.

You have to define functions before using them

Yes, like in good old C.

You can still have type-related bugs

Having a powerful compiler that catches 99% of the type bugs can be too relaxing and it becomes easier to miss that one occasional bug that slips through the compiler checks.

Bonus

We are maintaining a public code style guide for ReasonML, contributions are very welcome!


Original Link: https://dev.to/tpom6oh/reasonml-rescript-in-5-minutes-58l6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To