ReasonML vs BuckleScript
The title is quite misleading since ReasonML is a language while BuckleScript is a tool that transpiles(a.k.a. transpiler) OCaml in to JavaScript.
ReasonML is merely a syntactical change of OCaml. i.e. ReasonML semantic has a bijection to OCaml semantic.
Why using ReasonML then?
It is closer to JavaScript syntax. This might helps interop easier for developers since they don’t have to switch their brains and switch between the two languages.
As you can see, the structure are the same but ReasonML look less scary to JavaScript developers.
ReasonML also provides a more natural way to define a JavaScript object while doing so in OCaml requires BuckleScript annotation.
The result from above is jsObj
will be of type Js.t
(JavaScript Object) provided by BuckleScript. ReasonML uses ppx
rewriter to transform this to a valid OCaml type.
This is because OCaml treats {}
as a record construct, hence to get JavaScript object as a result we must hint a compiler. The [%bs.obj ... ]
is an extension node
to tell BuckleScript that we want to treat a code inside as JavaScript Object. The compiler will then expand the node using ppx
rewriter in the same manner as ReasonML. The result of two snippets above are identical as show below:
Trivial limitation
One limitation that you are likely to find funny is that you can’t directly define an empty JsObject using neitherlet x = {};
on ReasonML nor let x = [%bs.obj {}]]
. This is a limitation of the type inference itself. Because the empty object can be of any type, so we must use Js.Obj.empty()
instead(ref).
React-support
Personally, I prefer OCaml syntax. However, ReasonML is come in very handy when I write a web in React. it provides builtin React-style element support via ReasonReact. Moreover, modules from ReasonML and OCaml work together seamlessly. Practically, I tend to write a business logic in OCaml syntax while use ReasonML to handle React code.
In conclusion, ReasonML syntax lifts off some of the syntactic cumbersome for developers. It is unreasonable to not to use it when we want to develop React web with a BuckleScript. Likewise, it is also unreasonable if we try to use ReasonML ignoring the existence and roles of BuckleScript/OCaml.