BT

InfoQ Homepage News Facebook Overhauls ReasonML Syntax in Reason 3

Facebook Overhauls ReasonML Syntax in Reason 3

Bookmarks

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

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.