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?
Asked
Active
Viewed 3.1k times
81

Stefan Kendall
- 66,414
- 68
- 253
- 406
-
21`:load /path/to/file` in scala REPL – Jamil Sep 12 '11 at 04:55
-
1Awesome, that's exactly what I was looking for. Don't know why it was so hard to find. – Sep 12 '11 at 05:29
-
1Jamil, please, post this as an answer, so that Bea Metitiri could mark it as an answer. – Illarion Kovalchuk Sep 12 '11 at 08:59
3 Answers
102
type :load /path/to/file
in Scala REPL.
You can get complete list of available commands by typing :help

Jamil
- 2,150
- 1
- 19
- 20
-
4This seems to evaluate it one definition at a time, which doesn't work if the file has a sealed trait or forward references. `:paste /path/to/file` works better, as mentioned in Suresh's answer. – Brian McCutchon Sep 10 '17 at 21:07
24
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

erip
- 16,374
- 11
- 66
- 121

Suresh Babu
- 241
- 2
- 3
4
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

Esmaeil MIRZAEE
- 1,110
- 11
- 14
-
That's only a problem because Scala doesn't know how to expand `~` to your home directory. – Shane Bishop Jan 25 '21 at 23:40
-
relative paths are supported, too. So if you do `:load foo/bar/baz.scala`, it works, at least of today. – 18446744073709551615 Aug 25 '22 at 08:27