BT

Your opinion matters! Please fill in the InfoQ Survey!

Facebook Overhauls ReasonML Syntax in Reason 3

| by Sergio De Simone Follow 5 Followers on Nov 10, 2017. Estimated reading time: 1 minute |

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 Stage
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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT