Grimoire-
Command
.es

GNU+Linux command memo

ReasonML native compilation with Dune

Compiler du ReasonML avec Dune. Dune est un outil d’aide à la compilation de grand projets pour OCaml.

Information mostly took from : joshbranchaud.

1. Natively compile ReasonML

Dune is “a composable build system for OCaml” with out-of-the-box support for ReasonML. Dune can be used for a lot of things, but in simplest terms it can be used to compile ReasonML programs into native executables.

Considering the following ReasonML program :

/* hello_reason.re */
print_endline("Hello, Reason!")

We can then create the following Dune build file (named dune) :

;; dune
(executable
 (name hello_reason))

Then running the dune build hello_reason.exe command will compile the hello_reason.re into a hello_reason.exe executable binary that can be found in ./build/default.

We can also run dune exec hello_reason.exe which will firstly build the .exe if needed (if the sources have been updated).

Read more in the Quickstart guide of Dune.

2. Use OCaml libraries in your program

For instance, if you need an OCaml library in your ReasonML program, instruct Dune about it :

(executable
 (name hello_reason)
 (libraries yojson)
)

We should really provide the library :

$ eval (opam env)
$ opam install yojson

Now we can use the Yojson library in our code :

// month_nb.re
open Yojson.Basic
open Yojson.Basic.Util
let month_nb_json = from_file ("../../month_nb.json")
let () = print_endline(show(member("comment", month_nb_json)))
$ dune exec month_nb.re
`List ([`String ("Name: month_nb.json");
         `String ("Desc: contains the regexp tree of month number deternimation by month_nb.js");