Search code examples
ocamlutop

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…


Solution

  • 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.