25

Is it possible reload modules in OCaml's utop?

My development workflow goes something like this when playing around with OCaml code:

First, open up a file example.ml in Vim and hack on it. Switch to another terminal and run utop. From within utop, load the code with the directive #use "example.ml";;.

Go back to Vim and make some changes in the code. After changing the code, I want to play around with the new changes. Go back to utop and rerun the directive #use "example.ml".

It would be nice if there were a #reload directive that would reload all previously loaded modules, but there doesn't appear to be. Is there some way to easily reload all previously loaded modules?

Ideally this would work similarly to GHCi's :reload command.

glennsl
  • 28,186
  • 12
  • 57
  • 75
illabout
  • 3,517
  • 1
  • 18
  • 39
  • 1
    Assuming that the modules don't change between two runs. Is it possible to pass object files to utop? For example, you have a file tree.ml that you compiled to tree.cma (or tree.cmo), can you load the tree module with the command line "utop tree.cma". If so, you would just have to restart utop with the right options. The default is that it might not work that great inside emacs. – 永劫回帰 Sep 05 '17 at 17:40
  • In the greeting message from `#use "topfind";;`, it says: `... Topfind.reset();; to force that packages will be reloaded ...` – dubiousjim Jan 10 '20 at 15:44
  • You could put all your `#use`s in a `.ml` file and `#use` it each time you want to reload everything in it – Naeio Jul 30 '21 at 11:49

1 Answers1

2

At the time this question was asked, I believe dune had not yet established itself as the build system for OCaml. Using dune, you can simply launch a utop instance with locally defined libraries loaded (as explained here) by doing :

$ dune utop <dir> -- <args>

Where <dir> is a directory under which dune will search (recursively) for all libraries that will be loaded

ghilesZ
  • 1,502
  • 1
  • 18
  • 30