Sventimir
Sventimir

Reputation: 2056

Print values nicely in utop

Haskell's ghci can print nicely any value belonging to a type that implements Show typeclass. Is there any equivalent in OCaml that lets utop automatically print values of user-defined types nicely? Typing x |> M.to_string every line is quite tedious…

Upvotes: 2

Views: 1178

Answers (1)

Daniel Bünzli
Daniel Bünzli

Reputation: 5167

In your module for your type M.t define the function:

val pp : Format.formatter -> t -> unit

You can then install a printer for the type in ocaml or utop using:

# #install_printer M.pp

See the toplevel directive section of the manual.

Upvotes: 2

Related Questions