How to reload a class or package in Scala REPL?

后端 未结 5 1055
面向向阳花
面向向阳花 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:22

    This works for me....

    If your new source file Test.scala looks something like this...

    package com.tests
    
    object Test {
      def hello = "Hello World"
      def goodbye = "Goodbye, Cruel World"
    }
    

    You first have to load the new changes into Scala console (REPL).

    :load src/main/scala/com/tests/examples/Test.scala
    

    Then re-import the package so you can reference the new code in Scala console.

    import com.tests.Test
    

    Now enjoy your new code without restarting your session :)

    scala> Test.goodbye
    res0: String = Goodbye, Cruel World
    

提交回复
热议问题