DEV Community

Cover image for Reason got an Update
K
K

Posted on

Reason got an Update

Cover image by GotCredit on Flickr, cropped by me.

As you may heard, Facebook is out to do its own thing on functional programming languages. They created Reason, a language with the power of OCaml, but a more JavaScript like syntax.

Now this new language was lifted to version 3 that came with a bunch of syntax updates.

    let myFunction myArgument => ...
Enter fullscreen mode Exit fullscreen mode

becomes

    let myFunction = (myArgument) => ...
Enter fullscreen mode Exit fullscreen mode

Which looks a bit saner coming from JS, I think. The old version somehow looked a bit like a mix of a regular function definition where let is used instead of function and a arrow function definition, all without parenthesis.

Also, there are changes on the functions call site, more parenthesis for everyone!

    myFunction "Hello";
Enter fullscreen mode Exit fullscreen mode

becomes

    myFunction("Hello");
Enter fullscreen mode Exit fullscreen mode

I did a few projects in LiveScript, back in the pre-ES2015 days, and grew rather fond of the minimal syntax, but I have to admit, often parenthesis help to make things a bit clearer.

The arrow functions got an update too.

    {
      add: fun a b => a + b
    }
Enter fullscreen mode Exit fullscreen mode

becomes

    {
      add: (a, b) => a + b
    } 
Enter fullscreen mode Exit fullscreen mode

Again, a question of taste, but yes if you know JavaScript, you'll pretty much feel at home here.

Then there are the named arguments. They are a bit like when you make a function in JavaScript, that takes an object instead of multiple arguments, so you don't have to adhere to the position of the arguments and see on call site what each argument is called.

    let myFunction ::url ::method => ...
Enter fullscreen mode Exit fullscreen mode

becomes

    let myFunction = (~url, ~method) => ...
Enter fullscreen mode Exit fullscreen mode

On the call site it was changed too.

    myFunction ::url="http://dev.to" ::method="POST";
Enter fullscreen mode Exit fullscreen mode

becomes

    myFunction(~url="http://dev.to", ~method="POST");
Enter fullscreen mode Exit fullscreen mode

String concatination is now ++ so

    "abc" ^ "def"
Enter fullscreen mode Exit fullscreen mode

becomes

    "abc" ++ "def"
Enter fullscreen mode Exit fullscreen mode

Different from the JavaScript version, but a bit closer to + I guess.

The negation operator also got a more JavaScript like representation

    not expression;
Enter fullscreen mode Exit fullscreen mode

becomes

    ! expression;
Enter fullscreen mode Exit fullscreen mode

Also, calling a function without parameters required to pass it () which kinda looked like calling it in JavaScript even tho the concept seemed to be a bit different, but there was a space between the function name and the ()

    myFunction ();
Enter fullscreen mode Exit fullscreen mode

becomes

    myFunction();
Enter fullscreen mode Exit fullscreen mode

A minor change, but these tiny seemingly useless spaces probably gave people the heebie jeebies.

Conclusion

Reason moves more and more from OCaml to JavaScript, so starting with it has never been easier. Many people have complained about getting rid of the cool OCaml syntax and cluttering it with JavaScript trash, but I think this syntax convergence is a must for adoption.

Also, OCaml doesn't go away and I already heard that people started with Reason and switched to OCaml later, because they found the syntax more light weight.

Top comments (8)

Collapse
 
woolibrary profile image
JohnLeyo

Thank You

Collapse
 
kayis profile image
K

You're welcome.

Collapse
 
sammyisa profile image
Sammy Israwi

You said that starting with Reason has never been easier, but the reason (heh) why I haven't touched Reason yet is not because of the syntax; it's because I can't see a clear path from Reason to Web applications.

I'm excited to see different paradigms and languages being used for the web, I hope it becomes easier in the future to "pick your own tool"

Collapse
 
kayis profile image
K

Often you just need to get a to-js-compiler working. When you get your JS out, everything else follows.

Collapse
 
eljayadobe profile image
Eljay-Adobe

For people that prefer the OCaml style over the changes in Reason to make it more JavaScript-esque, one could consider Elm.

Collapse
 
kayis profile image
K

Yes, Elm is nice too.

But I had the impression that the Elm ecosystem is rather self-contained with its run-time etc.

Collapse
 
eljayadobe profile image
Eljay-Adobe

Elm also has JavaScript Interop, when there is a reason to bridge out of Elm's self-contained ecosystem.

Probably a lot of caveats. I haven't needed to do that, since I've only done toy Elm programs for fun.

Thread Thread
 
kayis profile image
K

I read that it is a bit of a struggle to make Elm work with JS libs, that's probably one of the reasons why FB build it's own language.

As far as I know, they did some things with SML and OCaml before, even React and they had good experience, but JS devs didn't like it, so they went for their own thing.