Skip to content

proof-of-concept to auto-install printer functions in the OCaml toplevel

License

Notifications You must be signed in to change notification settings

let-def/autoprinter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Autoprinter

A proof-of-concept to auto-install printer functions in the OCaml toplevel

Install with opam pin add autoprinter https://github.com/let-def/autoprinter.git.

Then #require autoprinter in the ocaml toplevel or in utop-full to setup a hook that will automatically install printers. This is done for any function that has a [@toplevel_printer] or [@ocaml.toplevel_printer] attribute.

For instance, entering the definition:

module X = struct
  type t = ...
  let print [@toplevel_printer] = ...
end

will also install X.print without having to manually execute #install_printer X.print;;.

This is similar to what utop (ocaml-community/utop#269) and auto-printer (https://github.com/rgrinberg/auto-printer) do, but the version in this repository also works on function instantiations.

Caveats

Because of toplevel design and particularly the expunge feature (see dbuenzli/rresult#5, ocaml/ocaml#6704, current state: ocaml/ocaml#7589), the loading is a bit complicated. With ocaml toplevel, it will work as expected after printing a scarry message:

# #require "autoprinter";;
...
Exception:
Invalid_argument
 "The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".
...

It won't work with utop:

# #require "autoprinter";;
Error: Reference to undefined global `Ident'

You should instead use the utop-full binary.

TODO: (though I am unlikely to do it :P)

  • fix the logic in utop
  • consider upstreaming the logic to the toplevel

About

proof-of-concept to auto-install printer functions in the OCaml toplevel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published