Simon Bundgaard-Egeberg
Posted on August 9, 2019
Why a new language?
A compiled language has many benefits. One of them being able to control the output.
ReasonML is not its own thing, but more of a syntax on top of OCaML. It is no secret that ReasonML was created to write stronger and more typesafe web applications in React. When thinking about the concepts of React ( Immutability, Props) it is actually not so befitting JavaScript as a language.
There has been made measures to alleviate this, like proptypes and Immutable JS, but this is more a treatment of the symptoms, and not the underlying disease.
Now, don't get me wrong, i love JavaScript. And i love technologies which utilize what JavaScript is, but it feels like when React tried to put the JavaScript hat on, it was more like putting shoes on a Orca (Which i imagine can be done, but looks quite silly and would require loads of duct tape).
I do not want to learn a new language said the angry JavaScript Developer.
But what if i told you that ReasonML looks much like JavaScript, but enables you to write truly typesafe code
Lets see how this looks with a simple addition function
let addition = (a, b) => a + b
const addition = (a, b) => a + b
Looks pretty much the same right? So what is the difference?
Well, since JavaScript is evaluated at runtime, this types of a
and b
could be whatever, and if it were a string or a boolean, some weird JavaScript things would happen
But hey mister blog man!?
The ReasonML code is just the same? How is that better then?
Because it compiles before it can be run. This means that the types will be inferred to integers, and the compiler will be angry if you use this function and give it a string.
But how?
Well, in reason there is different operators for different types, and +
is the operator for adding integers together, this is why the compiler can infer that it needs to integers. Had i written +.
the types would be inferred to floats.
Also, since the language is compiled, you are able to switch compilers. The "standard" ReasonML compiler is bucklescript, which compiles to JavaScript so it can run in the browser. There is, however, compilers that compile down to bytecode than can run on any OS natively. Which enables a Reason developer to write all kinds of neat native apps, using their good ol' skills of the web. This part of Reason is however still very young, and not ready for widespread production use.
The differences
There is things in reason that do not map 1-1 to JavaScript. One of these would be the switch.
In ReasonML the switch is a all powerful pattern matching tool that will help you do all kinds of nice conditional logic with zero to none in terms of overhead.
Just to throw out the "but switches in javascript sucks" argument i will show what a simple ReasonML switch compiles to in JavaScript.
And what better example is there than FizzBuzz
let fizzbuzz = (i) =>
switch (i mod 3, i mod 5) {
| (0, 0) => "FizzBuzz"
| (0, _) => "Fizz"
| (_, 0) => "Buzz"
| _ => string_of_int(i)
};
Now, when we talk about reading compiled code, we usually shudder at the thought. But when it comes to compiled ReasonML, it is not that bad.
function fizzbuzz(i) {
var match = i % 3;
var match$1 = i % 5;
if (match !== 0) {
if (match$1 !== 0) {
return String(i);
} else {
return "Buzz";
}
} else if (match$1 !== 0) {
return "Fizz";
} else {
return "FizzBuzz";
}
}
Even though the output is very verbose, it is very easy to read. I will leave analyzing the differences between the two to you, the reader, but i will just hint that _
is a wildcard and |
is a break in statements.
Next week i will show you how i made a Knitting app for my wife using ReasonML and ReasonReact.
Posted on August 9, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 7, 2022