Like in the Wizard of Oz, I’ve been on a journey in search of… confidence. Not in myself, but in my code. In this article series, I’d like to share answers to questions I come across as a JavaScript/React developer migrating to Reason.

What is Reason?

Reason, created by the same person that originally created React, is a new syntax for the OCaml language that is meant to be welcoming to JavaScript developers. Reason lets you write simple, fast and quality type safe code while leveraging both the JavaScript & OCaml ecosystems.

Why do we need Reason when we have React?

React was a stepping stone to Reason. Jordan Walke, the creator of React, started the concept of Reason before React. At that time, the compile-to-JS landscape was not as mature as it is now. Additionally, when the idea for React was first prototyped in SML, the JavaScript community was not ready for a functional paradigm. Four years later, they now believe the time is right for Reason.

As of 7/27/17, 50% of messenger.com’s UI Components have been converted to Reason.

Reported by Cheng Lou on Discord

Furthermore, with reason-react, you can actually write Reason code that compiles to idiomatic ReactJS. This allows for incremental adoption. Here is a great reason-react example to play with and to see how great the compiled output looks.

Why do we need types when we have tests?

Although I do think today’s JavaScript community more or less understands the need for a static type system, I can remember a time when I did not.

If you have types, and you have tests, your tests will be able to express much more with less energy.

Why do we need OCaml’s type system when we already have TypeScript/Flow?

This is a big one.

Reason/OCaml’s type system is sound; TypeScript’s is not. This means that if a Reason/OCaml program compiles, then you are guaranteed (in the mathematical sense) that you will not see runtime type errors (i.e. undefined is not a function).

As I understand, Flow’s type system is much better than TypeScript’s — but it’s important to note:

Flow is built with OCaml Flow is backed by Facebook

If Flow fully solved JavaScript’s problems, then I believe it’s not likely that Facebook would have also backed Reason.

From the Flow docs:

The idea of using types to manage code evolution and growth in JavaScript (and related languages) is not new. In fact, several useful type systems have been built for JavaScript in recent years. However, type systems differ in their goals. On one end of the spectrum are permissive type systems that provide some degree of linting against likely errors, without regard for correctness. On the other end of the spectrum are restrictive type systems that can guarantee the correctness of static code optimizations, at the cost of interoperability.

This brings us to our next question.

What about Reason’s interoperability with JavaScript?

It’s great! The Reason docs can get you started and this well-written blog post can take you further. The BuckleScript manual goes fully in-depth on all the amazing features that are available.