DEV Community

Cover image for ReasonML & Rescript in 5 minutes
Alex Verein
Alex Verein

Posted on • Originally published at avo.app

ReasonML & Rescript in 5 minutes

What is this language about?

It's a functional language that compiles to JavaScript (or to OCaml). We use it widely at avo.app.

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>
}
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (4)

Collapse
 
calag4n profile image
calag4n

Nice post.
React-Rescript is really interesting me, I'll give it a try as soon as I can.

Collapse
 
tpom6oh profile image
Alex Verein

Yeah, definitely worth a try!

Collapse
 
johnkazer profile image
John Kazer

I'd like to give them a go but time limits mean I'm gonna keep going with clojurescript first. Be interested in views of how they compare.

Collapse
 
tpom6oh profile image
Alex Verein

Unfortunately I don't have experience with closurescript, but would like to learn