This is an excerpt from the Scala Cookbook. This is Recipe 14.2, “How to paste and load blocks of code in the Scala REPL.”
Problem
You want to experiment with some code in the Scala REPL, and typing it in or trying to paste it into the REPL won’t work.
Solution
The REPL is “greedy” and consumes the first full statement you type in, so attempting to paste blocks of code into it can fail. To solve the problem, either use the :paste
command to paste blocks of code into the REPL, or use the :load
command to load the code from a file into the REPL.
The :paste command
Attempting to paste the following if/else block into the REPL will cause an error:
if (true) print("that was true") else print("that was false")
But by issuing the :paste
command before pasting in the code, the code will be interpreted properly:
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
As shown, follow these steps to paste your code into the REPL:
- Type the
:paste
command in the REPL. - Paste in your block of code (Command-V on a Mac, Ctrl-V on Windows).
- Press Ctrl-D, and the REPL will evaluate what you pasted in.
The :load command
Similarly, if you have source code in a file that you want to read into the REPL environment, you can use the :load
command. For example, assume you have the following source code in a file named Person.scala in the same directory where you started the REPL:
case class Person(name: String)
You can load that source code into the REPL environment like this:
scala> :load Person.scala Loading /Users/Al/ScalaTests/Person.scala... defined class Person
Once the code is loaded into the REPL, you can create a new Person
instance:
scala> val al = Person("Alvin Alexander") al: Person = Person(Alvin Alexander)
Note, however, that if your source code has a package
declaration:
// Person.scala source code package com.alvinalexander.foo case class Person(name: String)
the :load
command will fail:
scala> :load /Users/Al/ProjectX/Person.scala Loading /Users/Al/ProjectX/Person.scala... <console>:1: error: illegal start of definition package com.alvinalexander.foo ^ defined class Person
You can’t use packages in the REPL, so for situations like this, you’ll need to compile your file(s) and then include them on the classpath, as shown in Recipe 14.3, “Adding JAR Files and Classes to the REPL Classpath”.
Discussion
Although the REPL is incredibly helpful, its greedy nature can cause multiline statements to fail. Imagine that you want to type the following block of code into the REPL:
if (true) 't' else 'f'
If you try typing this code in one line at a time, the REPL will cut you off as soon as it sees a complete statement:
scala> if (true) | 't' res0: AnyVal = t
In this simple example, you can get around the problem by adding curly braces to the expression, in which case the REPL recognizes that the expression isn’t finished:
scala> if (true) { | 't' | } else { | 'f' | } res0: Char = t
But you can’t always do this. In the cases where this fails, use one of the approaches shown in the Solution.
Scala’s -i option
Another approach you can use is to load your source code with the -i
argument when starting the Scala REPL. See Recipe 14.4, “Running a Shell Command from the Scala REPL” for more information on that approach.
See Also
- Recipe 14.3, “Adding JAR Files and Classes to the Scala REPL Classpath”
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |