BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Reasons to Use ReasonML - Anton Tuzhik at ReactiveConf 2019

Reasons to Use ReasonML - Anton Tuzhik at ReactiveConf 2019

Bookmarks

Anton Tuzhik, recently presented a talk at ReactiveConf 2019 in Prague that detailed the reasons why he thinks ReasonML is a good option for implementing applications. ReasonML, while following a JavaScript-like syntax, does not suffer from JavaScript shortcomings. ReasonML supports both native and JavaScript as compile-targets, provides a sound type system, encourages immutable data structures, and has good interoperability with the JavaScript ecosystem.

Tuzhik started with reminding the audience of the advantage of JavaScript. With JavaScript, developers may prototype an application very quickly, it enables tremendous flexibility in the code, and is a rather forgiving language. Unfortunately, this comes at the cost of runtime errors such as "undefined is not a function". Tuzhik then quoted a list of compile-to-JavaScript languages (like TypeScript, Dart or ReasonML) that are seeking to solve the issue of type-related runtime errors.

ReasonML is a syntax extension for the OCaml language created by Facebook. OCaml, which has been around since the late 1990s, defines itself as an industrial-strength programming language that supports functional, imperative, and object-oriented styles. OCaml features user-definable algebraic data types and pattern-matching.

Indeed, ReasonML, built on top of OCaml, also supports both functional and imperative styles of programming. Most of the data structures in Reason are immutable by default. Variables declared with the keyword let cannot be reassigned. The same applies to fields in records (the equivalent of an object in Reason). However, Reason also allows developers to explicitly declare variables as mutable:

/* immutable variable */  
let num =  1;  
/* mutable variable */  
let mutableNum =  ref(1); 
/* Reassign the value of the variable */
mutableNum :=  2  

The same applies to fields in records:

type record = { 
  a: int, 
  mutable b: int,
}
let myRecord = { a: 1, b: 2 };
myRecord.b = 4; /* We can change b, but not a! */

ReasonML has, like OCaml, a static, strong, and sound type system which uses type inference to limit the occurrences in which programmers have to annotate their source code. A ReasonML developer answered the question Good arguments for using ReasonML instead of Typescript? as follows:

Inference and ergonomics is the primary selling point for me. 100% soundness with a fraction of the annotations [TypeScript] forces you to write is a big thing [in my opinion].

A less known fact is that ReasonML supports both native and JavaScript as compile-targets, which brings developers closer to the vision “written once, run anywhere”. A Reddit user explains:

[ReasonML] compiles to native pretty much exactly as OCaml does. Reason is a dialect of OCaml and BuckleScript will convert OCaml things to JavaScript so you can do Reason to native and OCaml to JavaScript

Since Reason supports JavaScript as a compile target, it has also a way to ensure interoperability with existing JavaScript code via a FFI (foreign function interface). Interoperability, however, requires developers to assign types to untyped JavaScript for Reason’s strong type system to operate. Interoperability allows developers to speed up development by reusing battle-tested libraries.

In fact, as Tuzhik mentioned, because of this FFI interoperability with JavaScript, Reason already has React bindings. This is unsurprising as Reason was created by Ricky Vetter, a lead Facebook Messenger developer and author of ReasonReact. Facebook Messenger was one of the first applications that relied extensively on ReasonML. Benjamin Johnson explained in a blog article:

Similar to what they’re doing with React, Facebook tests all of the new additions to the language internally before they actually add the language. Personally, I love this about Reason — the language was created to solve real-world problems in production-grade applications.

Tuzhik ended his talk with mentioning the editor plugins available for ReasonML code editing that are compatible with the major code editors like Visual Studio, IntelliJ, Atom and Vim. Tuzhik also provided a list of major companies using ReasonML.

ReactiveConf is a yearly conference targeted at developers with talks addressing the latest technologies and trends in software development. ReactiveConf 2019 took place from Oct. 30 - Nov. 1, 2019, and is the fifth installment of the event.

Rate this Article

Adoption
Style

BT