How to reload a class or package in Scala REPL?

There is an alternative to reloading the class if the goal is to not have to repeat previous commands. The REPL has the command

:replay

which restarts the REPL environment and plays back all previous valid commands. (The invalid ones are skipped, so if it was wrong before, it won’t suddenly work.) When the REPL is reset, it does reload classes, so new commands can use the contents of recompiled classes (in fact, the old commands will also use those recompiled classes).

This is not a general solution, but is a useful shortcut to extend an individual session with re-computable state.

Note: this applies to the bare Scala REPL. If you run it from SBT or some other environment, it may or may not work depending on how SBT or the other environment packages up classes–if you don’t update what is on the actual classpath being used, of course it won’t work!

Leave a Comment