The rise of ReasonML

Published on 05 Jun, 2018 · 4 minutes read

Michele Riva

A brief history of ReasonML

Reason lets you write simple, fast and quality type safe code while leveraging both the JavaScript & OCaml ecosystems.

ReasonML is a new syntax for the OCaml programming languagecreated by Facebook.
If I had to describe OCaml in just a few words, I would say:

As you can see, these four arguments can create a lot of hype these days, where JavaScript libraries like React, Redux or new syntax like TypeScript, tries to increase JavaScript’s performance, maintainability and scaling.

ReasonML takes the advantages of both OCaml and JavaScript and can compile to both languages.

Compiling to JavaScript, thanks to the amazing BuckleScript compiler, means that you can run your ReasonML code on both browser and Node runtime.

Compiling to OCaml, means that your code can be compiled a second time to bytecode, assembly or native code.

ReasonML is a pretty new language, so it’s clear that still doesn’t have all the features you may need in order to write a complex application.
But there’s a good news: you can integrate both JavaScript and OCaml code in your ReasonML codebase, so you can take the advantage of using any npm library or write your own modules in OCaml.

Pretty impressive, isn’t it?

Static Typing

OCaml is a static typed programming language.
That means that you can finally throw away Flow and Typescript!
Ok, I’m just kidding, but static typing in ReasonML can be seriously painless (and very efficient).
Let’s do an example.
I’m gonna use rtop, which is an amazing interactive command line tool for ReasonML.


As you can see, I’ve created a let binding (in JavaScript I would say “variable declaration”) without writing any type, but ReasonML recognized that foo is a string.
This is called type inference.
Of course, I can always explicitly declare the type of any binding:


Pretty similar to TypeScript, isn’t it?

Functional Paradigm

One of the most hyped paradigms these days. Just think about Immutable.js, ClojureScript, PureScript, Elm, Redux, Ramda, Rxjs.
Everyone seems to go crazy for this paradigm and the reason is clear.
Functional programming helps you a lot with concurrency (just think about Erlang and its concurrency model), functions are more predictable and easier to test, and you are less likely to get some unexpected side-effects during runtime.


Take as an example the function above: using a simple map method, I’ve tried to reassign a value to foo binding. The compiler thankfully prevent this kind of behaviours.
But, like any other functional programming language, ReasonML shows other interesting features, like function curryng, an amazing pattern matching system, variants and much more.
Just take the following code as an example:


Guess what will this program output? “Hey John Doe!
Now imagine how powerful and useful can be this feature.


We’ve previously seen how the compiler reports errors. ReasonML compiler is rock solid and helps you a lot with debugging and optimization of your code.
But what does it compile?
Let’s see:


type person =
  | Professor(string)
  | Student(string)
  | Nobody;

let kid: person = Student("John Doe");

let payload =
  switch (kid) {
  | Professor(name) => "Hello Dr. " ++ name
  | Student(name) => "Hey " ++ name ++ "!"
  | Nobody => "Who are you?"



type person =
  | Professor of string
  | Student of string
  | Nobody
let kid: person = ((Student ("John Doe"))[@explicit_arity ])
let payload =
  match kid with
  | ((Professor (name))[@explicit_arity ]) -> "Hello Dr. " ^ name
  | ((Student (name))[@explicit_arity ]) -> "Hey " ^ (name ^ "!")
  | Nobody  -> "Who are you?"
let _ = print_string payload


'use strict';

var Block = require("stdlib/block");
var Pervasives = require("stdlib/pervasives");

var kid = /* Student */Block.__(1, ["John Doe"]);

var payload;

payload = typeof kid === "number" ? "Who are you?" : (
    kid.tag ? "Hey John Doe!" : "Hello Dr. John Doe"


exports.kid = kid;
exports.payload = payload;
/* payload Not a pure module */

The JavaScript above can be bundled with Rollup, WebPack or whatever you prefer and it’s ready to run on any browser!
Or, if you’re a brave person, you can compile the OCaml code to assembly or native code (take a look at this Gist).

Frontend Usage

ReasonML has been created by Jordan Walke, the same smart engineer who created React.js.
React’s prototypes in fact, were written in StandardML, a distant cousin of OCaml.
You can imagine how close can these two projects be. ReasonML has an official support for React, and some folks out there are giving support for this new OCaml syntax to Vue.
You can also compile to plain JavaScript code and bundle it to make it run on your browser without any framework, take this Super Mario game (written in ReasonML) as an example.


As we’ve seen, ReasonML is a language that’s worth learning.
It’s fun to write, fast to execute and has an awesome compiler.
There’s another big advantage to keep in mind: like JavaScript, ReasonML has the potential to become a full stack programming language, giving you the chance to write both frontend and backend code.
That means that a single developer can code both the server and the UI, breaking the line between backend software engineers and frontend developers.
I know that ReasonML is still young. But, if I were in your shoes, I’d give it a chance!