1. Introduction

Scala REPL is a command-line tool for the expression, evaluation, and execution of small code snippets. The acronym REPL stands for “Read-Evaluate-Print-Loop”.

Similar to Java Shell, Scala REPL is very useful to newcomers and to those who want to experiment with new libraries or language features.

2. Installation and Usage

Scala REPL comes along with Scala binaries, so we can use it as long as we have Scala installed. To start a Scala REPL session, we need to execute the “scala” command in a terminal. This is what we’ll see in our terminal once we start the REPL session:

Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.


3. Useful Commands and Features

Executing Scala in a terminal shell can come in handy, and REPL comes with a generous set of commands and features. In this section, we’ll demonstrate some of the most useful ones.

3.1. help Command

As expected, the :help command prints the list of available commands:

scala> :help
All commands can be abbreviated, e.g., :he instead of :help.
:edit <id>|<line>        edit history
:help [command]          print this summary or command-specific help
:history [num]           show the history (optional num is commands to show)
:h? <string>             search the history
:imports [name name ...] show import history, identifying sources of names
:implicits [-v]          show the implicits in scope
:javap <path|class>      disassemble a file or class name
:line <id>|<line>        place line(s) at the end of history
:load <path>             interpret lines in a file
:paste [-raw] [path]     enter paste mode or paste a file
:power                   enable power user mode
:quit                    exit the interpreter
:replay [options]        reset the repl and replay all previous commands
:require <path>          add a jar to the classpath
:reset [options]         reset the repl to its initial state, forgetting all session entries
:save <path>             save replayable session to a file
:sh <command line>       run a shell command (result is implicitly => List[String])
:settings <options>      update compiler options, if possible; see reset
:silent                  disable/enable automatic printing of results
:type [-v] <expr>        display the type of an expression without evaluating it
:kind [-v] <type>        display the kind of a type. see also :help kind
:warnings                show the suppressed warnings from the most recent line which had any

3.2. load Command

The :load command loads and executes a file of REPL code.

Let’s assume that we have the following file in the current path:

// replScript.scala
case class Person(name: String, age: Int)
val me = Person("Manolis", 34)

To run the file, we simply need to use the :load command:

scala> :load -v replScript.scala
Loading replScript.scala...

scala> case class Person(name: String, age: Int)
defined class Person

scala> val me = Person("Manolis", 34)
me: Person = Person(Manolis,34)


3.3. paste Command

One of the first obstacles we’ll stumble upon while using REPL is the fact that, by default, the code is evaluated line by line.

As a result, multi-line blocks of code, such as functions, can’t be evaluated without the :paste command.

3.4. lastException Binding

The last thrown exception is bound to lastException. Let’s throw a few exceptions and see how lastException works:

scala> throw new RuntimeException("REPL is fantastic!")
java.lang.RuntimeException: REPL is fantastic!
  ... 32 elided

scala> lastException
res4: Throwable = java.lang.RuntimeException: REPL is fantastic!

scala> throw new RuntimeException("REPL is awesome!")
java.lang.RuntimeException: REPL is awesome!
  ... 32 elided

scala> lastException
res6: Throwable = java.lang.RuntimeException: REPL is awesome!


3.5. reset Command

In case we need to clear variables or functions from our REPL session, we can use the :reset command.

After resetting a session, previous definitions are no longer available to use:

scala> val a = 1
a: Int = 1

scala> def foo(): String = "blah.."
foo: ()String

scala> :reset
Resetting interpreter state.
Forgetting this session history:

val a = 1
def foo(): String = "blah.."

Forgetting all expression results and named terms: a, foo

scala> a
<console>:12: error: not found: value a


3.6. silent Command

After defining a variable or a function, the definition is printed afterward. We can disable definition printing by using the :silent command:

scala> val a = 1
a: Int = 1

scala> :silent

scala> val b = 2


3.7. quit Command

Finally, we can close a REPL session via the :quit command or by pressing ctrl-D.

4. Working With Dependencies

Even though Scala REPL is not meant for big tasks or projects, we can add additional jars in the classpath to make our work easier.

4.1. require Command

The :require command adds jars to the classpath:

scala> :require commons-lang3-3.12.0.jar
Added '/Users/mvarvarigos/scala-repl/commons-lang3-3.12.0.jar' to classpath.


4.2. scala -cp

We can add jars as we launch REPL with the -cp argument:

scala -cp commons-lang3-3.12.0.jar

4.3. sbt Console

Another way to import additional dependencies into a REPL session is to use the sbt console within an sbt project. The sbt console command launches a REPL with every sbt dependency added to the classpath.

5. Conclusion

In this article, we illustrated some of the most used Scala REPL features and commands.

Comments are closed on this article!