Why am I building stuff in ReasonML and why should you?
let add = (a, b) => a + b; add(6, 9);
Since BuckleScript is based on OCaml compiler, you’ll get a blazingly fast compilation that is much faster than Babel and several times faster than TypeScript.
However, if you didn’t find your library, you’ll need to write Reason bindings yourself. If you’re just starting with Reason, keep in mind that writing bindings aren’t a thing you want to start with, since it’s one of the more challenging things in Reason’s ecosystem.
Fortunately, I’m just writing a post about writing Reason bindings, so stay tuned!
Although OCaml itself is a multi-paradigm language — combining functional with object-oriented programming — Reason put more emphasis on its functional programming side.
So, you can think of Reason as a functional programming language with immutability as one of its core principles. So, you get all these features: the immutable data structures, functional iteration methods for them, currying, piping, first-class functions, etc.
On the other hand, Reason also provides us with escape hatches, so we can perform side-effects and mutate our data. However, these are just opt-outs, so you’ll write such mutations explicitly using imperative syntax.
Reason is a statically typed language with a strong type system, which means it evaluates the types of your values during the compilation time. In other words, you don’t need to run your code to check if your types are correct.
Strong type system means clearer and safer code. Reason’s type system is superior to what you’d get from TypeScript or Flow (which is written in OCaml). In Flow, it’s quite easy to lie to your type system. As a result, you’ll often get only partial type coverage of your code.
You get reliable type safety from these libraries only if you’re very disciplined and if you understand the type system well. If you don’t have any experience with typed languages, a loose type system isn’t a good place to start learning how type system works.
Compared to these libraries, Reason’s type system requires less effort to maintain and you can rely on it 100% since it won’t allow you to write untyped code. As a result, you’ll get no runtime exceptions in production.
Having a type system doesn’t mean you need to explicitly define types for all your values. If you don’t want to write types manually, the compiler will figure out (infer) them for you. For example, if you’re using a value as an
integer, the compiler will set the type to the
let plus = (a, b) => a + b /* type inference by compiler */ (int, int) => int
This enables you to write your code faster without losing benefits of static typing.
The advantages of types are most visible in the case of functions. If you know the types of your parameters, it’s much easier to understand what the function does.
It might seem that writing types mean more code and complexity. However, that’s not the whole story, since the static types will force you to think differently about structuring your code and to find ways to simplify it.
Moreover, static typing in Reason will give you awesome developer experience since your favorite editor will provide you with assistance. You’ll get code auto-completion, integrated documentation, and various hints. And when your code compiles, everything will happen really fast because of the Reason’s powerful build system.
If you’re a newcomer, learning static types will take you some time. However, Reason will help you with that since it won’t compile your code if your types are wrong and it’ll provide you with readable warning and error messages.
You’ll get the formatter via your Reason code editor extension. How does it work? It takes your Reason code, parses it to the abstract syntax tree (AST) and rewrites it so it’s always consistent. Not only does it do code formatting for you, but it can also identify optimal constructs for your code. For example, if you pattern match for true or false, it’ll transform it to the ternary operator. Moreover, it’ll also help you to convert the old version of Reason syntax to the current one since it works on the AST level. If you want to learn more, check out this post by Cheng Lou.