How to reload a class or package in Scala REPL?

后端 未结 5 1061
面向向阳花
面向向阳花 2020-12-24 00:50

I almost always have a Scala REPL session or two open, which makes it very easy to give Java or Scala classes a quick test. But if I change a class and recompile it, the RE

5条回答
  •  眼角桃花
    2020-12-24 01:27

    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!

提交回复
热议问题