1. Overview

REPL is short for ‘Read-Eval-Print Loop’. It’s an interactive command-line environment that enables us to run interpreted programs on the fly. In addition, it provides an interface for us to run statements and evaluate expressions.

In this tutorial, we’ll learn how to use Scala’s REPL.

2. How to Install the Scala REPL

First, we need to install open-jdk. Scala relies on this to compile our code. Next, we’ll install Scala. The REPL comes bundled with our Scala installation.

We can perform this installation using the officially recommended tool called Coursier. It’s a development environment manager for Scala applications. We can use the instructions from Scala’s official page to load the latest version of Scala3 for a particular operating system and computer architecture using the Coursier tool.

We can verify that we’ve successfully installed Scala by checking the version on the command line:

$ scala -version
# Sample output: Scala code runner version 3.2.2 -- Copyright 2002-2023, LAMP/EPFL

3. How to Start and Quit a REPL Session

As described in its name, a REPL uses a read, evaluate, print, and loop mechanism. It reads the statement(s) or expressions given to it, evaluates it, and then provides feedback on the terminal.

The variables and results are only available for that session whenever we enter them into an interactive shell. If we close the terminal or exit the shell, all the data for that session will be available in subsequent sessions. However, a record of our previous session entries is stored, and we can navigate through these using the up and down arrow keys.

To start a REPL session, we type the command scala or scala3 in the command line. This starts a shell that begins with a chevron (>). The chevron indicates that the interpreter is ready to accept some input:

Welcome to Scala 3.2.2 (11.0.18, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> println(“Playing with Scala3!”)

The above shell interaction prints the text in the println function.

We can use CTRL + D or CTRL + C to quit the shell, depending on the operating system or computer. On some PCs, both keyboard shortcuts work. We can also use the :quit command to exit the REPL shell.

4. Advantages of Using a REPL

A REPL provides us with an environment to quickly test code. For example, if we have some new libraries or classes we want to try, a REPL shell would be a perfect environment.

If we’re also just newly learning the Scala language, then the REPL is a great place to start from. This is because we get interactive results with each complete statement. Also, there’s no need to first package code in a file to run it.

5. Conclusion

In this article, we learned that Scala installation ships with a REPL. We also discussed how to run the Scala command to start a new session. Finally, to quit the session, we saw that we can enter :quit in the shell.

Inline Feedbacks
View all comments