In short, use the :load
function in the scala REPL to load a file. Then you can call that function in the file if you wrap it in an object or class since sbt
tries to compile it. Not sure if you can do it with just a function definition.
Wrap it in an object
to get sbt
to compile it correctly.
object Times{
def timesTwo(i: Int): Int = {
println("hello world")
i * 2
}
}
Load the file:
scala> :load Times.scala
Loading Times.scala...
defined module Times
Then call timesTwo
in Times
:
scala> Times.timesTwo(2)
hello world
res0: Int = 4
If you want just the function definition without wrapping it in a class
or object
the you can paste it with the command :paste
in the scala REPL/sbt console.
scala> :paste
// Entering paste mode (ctrl-D to finish)
def timesTwo(i: Int): Int = {
println("hello world")
i * 2
}
// Exiting paste mode, now interpreting.
timesTwo: (i: Int)Int
This can be called by just the function name.
scala> timesTwo(2)
hello world
res1: Int = 4