Hello World with OCaml and ReasonML

idkjs

Alain

Posted on January 12, 2020

Hello World with OCaml and ReasonML

A note to self on using OCaml with the ReasonML syntax without Bucklescript.

Setup

  1. Install opam
  2. Install reason language options with opam install reason

HelloWorld

Create a hello.re file:

// hello.re
print_string("Hello world!\n");

Compile hello.re by running ocamlc -o hello -pp "refmt -p ml" -impl hello.re.

Open your terminal and run ./hello.

Your output is:

➜  helloworld ocamlc -o hello -pp "refmt -p ml" -impl hello.re
➜  helloworld ./hello
Hello world!

Sources

https://riptutorial.com/ocaml/example/7096/hello-world

repo

Notes from Discord Channel:

@idkjs the ocaml toolchain read only ocaml syntax, binary ast (which is shared between reason/ocaml). Hopefully the reason package come with a tool that is able to translate both syntax refmt.
the -p option of refmt is print. So refmt -p ml hello.re > hello.re.ml take the reason syntax in the re file and output in the hello.re.ml file. I use -p ml for debugging but -p binary is used by dune because it is quicker. Then you can use OCaml binary ocamlc -o my_target hello.re.ml (the -o is for ouput default name is machine dependant). because file end with .ml the compiler know it must build .cmo/.exe, if passed a .mli it will build .cmi, etc. The problem with .re file is it is not .ml nor .mli so we need to say it will be a implementation -impl or a interface -intf. and finally -pp is run this command on this file to have a valid ocaml file (here we use refmt because it output valid OCaml)

-impl <file>  Compile <file> as a .ml file
-intf <file>  Compile <file> as a .mli file
-intf-suffix <string>  Suffix for interface files (default: .mli)
-pp <command>  Pipe sources through preprocessor <command>
💖 💪 🙅 🚩
idkjs
Alain

Posted on January 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related