TECH Insights

Viden og indsigter fra IT Minds' it-talenter

What is the Reason?

Skrevet af Mindster og Software Developer hos IT Minds on 19-08-19 12:37
Mindster og Software Developer hos IT Minds

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

Skærmbillede 2019-08-19 kl. 11.55.33

Skærmbillede 2019-08-19 kl. 11.55.42

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 FizzBuzzSkærmbillede 2019-08-19 kl. 11.57.22

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.

Skærmbillede 2019-08-19 kl. 11.58.25

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.

Insights fra de bedste softwaretalenter

En del af IT Minds’ DNA er, at vi spotter og tiltrækker de bedste softwaretalenter, som er stærke i de nyeste teknologier og frameworks. Derfor vil vi gerne dele ud af vores viden og erfaringer vores konsulenter opsamler, når de afprøver og arbejder med nye teknologier og frameworks.

Seneste indlæg