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 call…


This content originally appeared on DEV Community and was authored by Alex Verein

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!


This content originally appeared on DEV Community and was authored by Alex Verein


Print Share Comment Cite Upload Translate Updates
APA

Alex Verein | Sciencx (2021-04-23T16:46:57+00:00) ReasonML & Rescript in 5 minutes. Retrieved from https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/

MLA
" » ReasonML & Rescript in 5 minutes." Alex Verein | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/
HARVARD
Alex Verein | Sciencx Friday April 23, 2021 » ReasonML & Rescript in 5 minutes., viewed ,<https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/>
VANCOUVER
Alex Verein | Sciencx - » ReasonML & Rescript in 5 minutes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/
CHICAGO
" » ReasonML & Rescript in 5 minutes." Alex Verein | Sciencx - Accessed . https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/
IEEE
" » ReasonML & Rescript in 5 minutes." Alex Verein | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/. [Accessed: ]
rf:citation
» ReasonML & Rescript in 5 minutes | Alex Verein | Sciencx | https://www.scien.cx/2021/04/23/reasonml-rescript-in-5-minutes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.