OCaml

description: About OCaml.

lang: ENG

Opam

Opam is the package manager for OCaml.

Pin all the *.opam in a repo to a specific version without installing them

# -n = don't install .opam packages themselves
opam pin <path> --with-version <version> -n

Resources


Dune

Dune is a build system for OCaml.

Build a specific package

dune build -p <package>

Build some doc

dune build @doc

Run some tests

dune runtest

Build in watch mode and clear the screen on rebuilt

dune build -w --terminal-persistence=clear-on-rebuild

Resources


PPX

Summary

PPXs are a tool included in OCaml that allows you to rewrite the AST at compile time. It's very powerful but also very complicated.

Resources

OCaml

Note about exceptions

Exceptions can be a bit tricky to understand. It's hard to know when it is going to generate a StackOverflow error. With this two examples, we can understand how it behaves.

let rec g x =
    match x with
    | Leaf x -> raise (Found x)
    | Node (r, v) -> g r ; g v

let f x = try g x with Found x -> (* Do something *)
let rec g x =
    match x with
    | Leaf x -> raise (Found x)
    | Node (r, v) ->  try g r ; g v with Found x -> x

Resources