Load Scala file into interpreter to use functions?

ScalaInterpreter

Scala Problem Overview


I have some Scala functions defined in a file, not in a class, and I would like to use them in the Scala interpreter. I know I can say scala filename.scala to simply run the file and exit the interpreter, but I would like to run the file and then stay in the interpreter so I can do some testing. Can anyone tell me how to simply load a file into the interpreter so I can use the functions defined within it?

Scala Solutions


Solution 1 - Scala

type :load /path/to/file in Scala REPL.

You can get complete list of available commands by typing :help

Solution 2 - Scala

On occasions, :paste might be your better friend (than :load). Here is an example on how to use :paste.

scala> :paste
// Entering paste mode (ctrl-D to finish)

if (true)
  print("that was true")
else
  print("false")

[Ctrl-D]

// Exiting paste mode, now interpreting.

that was true

One can also use :paste to load a file using following command :paste [path]

scala> :paste ~/Desktop/repl_seeder.scala
Pasting file ~/Desktop/repl_seeder.scala...
defined object test1

scala> test1.main(Str)
my first scala program

Solution 3 - Scala

Just reminder, put the complete path. I found problem in Linux by doing like this:

> :load ~/fileName.scala

to get rid of error "That file does not exist" I did

> :load /complete/path/fileName.scala

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser550617View Question on Stackoverflow
Solution 1 - ScalaJamilView Answer on Stackoverflow
Solution 2 - ScalaSuresh BabuView Answer on Stackoverflow
Solution 3 - ScalaEsmaeil MIRZAEEView Answer on Stackoverflow