Search code examples
typesocamlread-eval-print-loopreason

Displaying type and value with Reason


The OCaml REPL displays the value and type of any expression. For instance, evaluating:

let rec map f = function
    | [] -> []
    | x::l -> f x :: map f l;;

Gives:

val map : ('a -> 'b) -> 'a list -> 'b list = <fun>

This is unvaluable for teaching the language.

I am considering switching to Reason, but how would you obtain the same informations?

let rec map = (f) =>
  fun
  | [] => []
  | [x, ...l] => [f(x), ...map(f, l)];

Try Reason doesn't display any type, and I am not sure if there exists a REPL for Reason.


Solution

  • rtop is a toplevel (REPL in OCaml-lingo) that ships with reason-cli, and that is really just a thin wrapper around utop. It'll print the type like this:

    let map: (('a) => 'b, list('a)) => list('b) = <fun>;
    

    In VSCode, merlin will also give you the type of let bindings in a "CodeLens" displayed above each binding.

    enter image description here