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 => ...
becomes
let myFunction = (myArgument) => ...
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";
becomes
myFunction("Hello");
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
}
becomes
{
add: (a, b) => a + b
}
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 => ...
becomes
let myFunction = (~url, ~method) => ...
On the call site it was changed too.
myFunction ::url="http://dev.to" ::method="POST";
becomes
myFunction(~url="http://dev.to", ~method="POST");
String concatination is now ++
so
"abc" ^ "def"
becomes
"abc" ++ "def"
Different from the JavaScript version, but a bit closer to +
I guess.
The negation operator also got a more JavaScript like representation
not expression;
becomes
! expression;
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 ();
becomes
myFunction();
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)
Thank You
You're welcome.
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"
Often you just need to get a to-js-compiler working. When you get your JS out, everything else follows.
For people that prefer the OCaml style over the changes in Reason to make it more JavaScript-esque, one could consider Elm.
Yes, Elm is nice too.
But I had the impression that the Elm ecosystem is rather self-contained with its run-time etc.
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.
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.