Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Facebook Overhauls ReasonML Syntax in Reason 3

Facebook Overhauls ReasonML Syntax in Reason 3

Leia em Português

This item in japanese

Reason, Facebook’s attempt to bring OCaml safety and speed to JavaScript developers, reaches version 3, which introduces new syntax and many fixes.

One of the most visible changes to Reason syntax is the use of the JavaScript application/abstraction syntax instead of Ocaml’s. This means that in Reason 3 you would call a function of two parameters by putting parenthesis around them, as you do in JavaScript:

myFunction(arg1, arg2) // new, C-like syntax
myFunction arg1 arg2   // old, OCaml-like syntax

Other notable changes are the following:

  • New syntax to declare the type of JavaScript objects using {. }, e.g.:

    type payload = {.  // no need to call Js.t here
      "name": string,
      "age": int
  • Extended support for type punning with labeled parameters and new syntax using ~ instead of ::. This is how you declare and call a function with labeled parameter:

    let addCoordinates = (~x, ~y) => {
      /* use x and y here */
    addCoordinates(~x=5, ~y=6);

    Compare this to the previous syntax:

    let addCoordinates x::x y::y => {
      /* use x and y here */
    addCoordinates x::5 y::6;

    Additionally, type punning (which, in the example above allows to write ~x instead of the more tedious ~x as x) can be used along with type annotations, which was not previously supported:

    let add = (~first: int, ~second: int) : int => first + second;
  • String concatenation can be now applied using the ++ operator instead of ^.

  • Logical negation is ! instead of not.

Developers who have an existing Reason code base should be not worry at the extent of the syntax changes, which were mostly driven by the idea of making it feel more “natural” for JavaScript developers. Indeed, Reason 3 comes with a migration script which promises to make the transition easier.

Another promising new feature of the Reason ecosystem is the availability of an official API to programmatically access the Reason parser, refmt, which already powers a number of tools like Klipse, reason-tools, the Reason docs site, etc.

Reason is Facebook attempt to bring a sound type system, OCaml’s type system, to the JavaScript environment. It uses BuckleScript at its core and provides a syntax geared to the likes of JavaScript programmers, who could find OCaml syntax a bit unfriendly.

Rate this Article