The Edge, Wit, and Courage of ReasonML
ReasonML has stolen my attention recently.
If I'm not at work or running about, I am probably digging into some ReasonML/OCaml repositories like an addict in the tenderloin.
Perhaps I like it for the unparalleled incremental build speed.
Perhaps I like it because its not too different from JavaScript yet it nets me a plethora of built-in advantages like type safety.
Perhaps I like it because of my affinity for math in school, and the functional paradigm gives me a warm fuzzy feeling inside.
There are a few reasons, no pun intended, for my love of Reason.
Just like a functional program wields function composition with immutable state to achieve its goal, Reason's composition of the best of OCaml and JavaScript make it a strong candidate for the future of programming.
~end wizard speak~
The Edge of OCaml
For starters, Reason is basically a syntax upgrade to OCaml. Let me start by describing how that helps us.
Speed
OCaml is a pretty damn fast language. It's incremental build times dwarf that of almost any other language.
Static Typing
Its type system is nearly impenetrable, and the compiler does a solid job of make you exhaust all of your options in branching logic.
...with Great Inference
It's powerful type inference cleans up a lot of the verbosity associated with annotating functions and variables with types.
Industry Proven
OCaml is a proven language of academics and industries where safety and mission critical code is paramount. This was one of the reason's it was adopted as the base for ReasonML.
The Wit of JavaScript
With Reason comes a lighter, more JavaScript-like syntax.
Syntax
Reason syntax is far easier to read and digest than that of OCaml, and most other functional languages for that matter.
Easy Build System
Its massively more accessible to newcomers than OCaml in that you probably wont spend days trying to install your dependencies properly. When using BuckleScript its often just another npm install <package>
.
### JavaScript Interop
If you need more training wheels when writing Reason, do not fear. You can use special directives to write JavaScript directly in your Reason code. Its not cheating, the language was designed this way to make migrating codebases easier.
NodeJS Too
The language that powers web applications can be used on the server with the help of NodeJS. The language being Reason of course. Since it transpiles to JavaScript, theres no reason you cannot simply write Reason which becomes server code.
You can write basically everything in your tech stack with JavaScript and in most cases it will be just fine. What motive would someone have to write Reason then, especially if they have built their likely profitable career on JavaScript?
The Courage of Reason
Over the past few weeks, I've been exploring writing Native Reason code. I think some of the projects that are being developed show great courage in showing off the potential of ReasonML.
While I certainly appreciate it's benefits on the frontend when transpiled to JavaScript, it would be super neat if I could write it on the server too.
I want to communicate with databases, make blazing quick command line tools, and write super efficient servers, perhaps serving GraphQL.
And I want to do it with the same language that I'm writing in for my web applications.
Currently, we can do this with JavaScript, but I think we can do better.
By further leveraging the OCaml ecosystem we can actually write incredibly robust servers and developer tools.
The Esy package manager allows us to install and build packages from npm or opam in a convenient package.json fashion without having to manually hack system dependencies.
As Esy further develops, Reason on the backend will become increasingly more accessible to developers looking to write stupidly fast native code.
If you're interested in what's already been made with Esy, check out the following projects:
- fnm, an alternative to nvm for managing your NodeJS version. It's much faster than nvm.
- fastpack, a ridiculously fast JavaScript bundler
- revery, a desktop UI framework like electron, except without downloading a whole browser onto your users' computer.
And there are more out there in the Forests of Open Source.
This certainly won't be my last post on this topic so stay tuned for more Reason evangelism on the horizon.