ReasonML advertises itself as Syntax and toolchain for OCaml. In this article, I'll try my best to explain what ReasonML offers to the OCaml toolchain.
#What is ReasonML?
#What do you mean by a new dialect of OCaml?
Take a look at this diagram (credit to Dr. Axel Rauschmayer):
OCaml's compiler is pretty unique, it's a set of pluggable parts that can be replaced and used together. This allows ReasonML to be a new syntax on top of OCaml toolchain. The only different between ReasonML and OCaml is the syntax. All semantics are the same.
refmt turns RE files into OCaml AST so any tools that works with OCaml would work with ReasonML syntax without modification. Some tools like dune (jbuilder) supports ReasonML out-of-the-box, which means it runs
refmt for you under the hood.
#Can I use ReasonML without
This question pops up a lot on Reason Discord channel. Beside turning RE files to OCaml AST,
refmt can pretty print your RE files (like gofmt or prettier). People often get confused this feature as
refmt's main functionality. You can disable the pretty print feature but you can't use ReasonML without
Bucklescript is a backend for OCaml and a seperate project from ReasonML. It was originally started by folks at Bloomberg. It compiles your ReasonML/OCaml code into optimized JS code. Even though being a seperated project from ReasonML, they are are often referenced at a unit. BuckleScript is a critical piece of the ReasonML experience for Web development.
#What is the different between
js_of_ocaml and Bucklescript?
Before Bucklescript, there was
js_of_ocaml (JSOO) which also compiles OCaml to JS. Even though they have the same goal, they target different use case:
- JSOO helps you compile your existing OCaml project to have it run in the browser with as little code modification as possible. In another words, JSOO targets OCaml folks.
- Bucklescript on the other hand, provide the best tool for interoping between ReasonML/OCaml and JS. It main target is JS folks.
Furthermore, Bucklescript and JSOO operates at different level in the compiler toolchain. You can learn more about the differences between the two of them in Bucklescript docs.
Bucklescript operates at higher level in the compiler tool chain than JSOO. This means JSOO could use advanced tools to do whole program tree-shaking (I forgot the name). Bucklescript on the other hand, outputting hints that JS bundlers could use for tree-shaking. This isn't ideal but it's already an incredible work by Bucklescript team. Thank you!
Side note: Bucklescript's Lead developer Hongbo Zhang have shared the ideas of Bucklescript before making it. So I can say that no wheels were invented in the process of making Bucklescript.
Bucklescript actually contains 2 parts: bsb - the build system and bsc - the compiler. They are often referenced as a single unit: Bucklescript to avoid churn. ReasonML bring bsb and bsb-native to the game. Here is some available build systems:
- bsb: Build system for JS projects that are composed of other packages that use bsb.
- bsb-native: Like bsb but also compiles those same kinds of projects to native.
- dune (jbuilder)
It would be a mistake to not mentioning the JS developers ReasonML is bringing to the ecosystem. This helps expanding the user base, push OCaml towards new landscape.
ReasonML is a promising project and it's moving really fast. If you want to sleep well at night, try ReasonML. The advanced type system inherited from OCaml with 20 years of development has made it the best option available. It eliminates an entire class of bugs in the JS world related to program types.
You can reach me via Twitter(@khoanguyenme) or @thangngoc89 on Discord channel.