2. The Scala Environment

Pankaj Patil
6 min readJan 14, 2022

The Scala REPL (Read-Eval-Print-Loop) is a development tool to interpret Scala code snippets. It will be a crucial tool in learning Scala, and it doesn’t require too much setup or infrastructure: you’ll be able to play and experiment with the language by typing and evaluating fragments of code.

You’ll also install the git and docker development tools on your machine, which are not specific to Scala but used in some of the more advanced capstones. In the next lesson, you’ll complete your machine’s setup by installing sbt — a tool to build and run structured Scala programs.

1. The REPL installation

First, you need to check that you have installed Java 8+ JDK. Open the terminal and type the command java -version to check your java version. You should see a message similar to the following:

$ java –version
openjdk version “15.0.1” 2020–10–20
OpenJDK Runtime Environment (build 15.0.1+9–18)
OpenJDK 64-Bit Server VM (build 15.0.1+9–18, mixed mode, sharing)

If you need to install or upgrade your JDK, you can find instructions on how to do so on the OpenJDK’s website (https://openjdk.java.net/install). You are now ready to download and install the Scala REPL; depending on your operating system, you will have different instructions to follow.

LINUX
On Ubuntu and other Debian-based distribution, type the following command in the terminal:
$ sudo apt-get install scala

If you are using an RPM-based distribution, such as Red Hat Enterprise, type the following instruction:
$ sudo yum install scala

Use emerge package manager if you are on Gentoo:
$ emerge dev-lang/scala

MACOS
On macOS, you can install Scala using Homebrew by executing the following command in your terminal:
$ brew install scala

If you prefer, you can also use MacPorts as follows:
$ port install scala

WINDOWS
Visit https://scala-lang.org/download to download the MSI installer
for Scala and open the file to complete the installation.

Once you have completed the installation, you should be able to execute the
command scala and receive a message similar to the following:
$ scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 15.0.1).
Type in expressions for evaluation. Or try :help.

scala>
The symbol scala> indicates that the Scala REPL is running and ready to receive your commands.

2. The REPL commands

A REPL tool can receive instructions and interpret snippets of code. Let’s see what the Scala REPL’s main commands.
The Scala REPL identifies commands by the prefix “:”; it considers any other instruction as Scala code snippets to compile and evaluate. Lots of commands are available, but you usually remember just a few of them. Here’s a list of the most useful commands:

  • :quit gracefully exits the interpreter:
    scala> :quit
  • :help lists all the commands available with a brief description. Use it to discover new commands or to check the syntax of an existing one:
    scala> :help

All commands can be abbreviated, e.g., :he instead of :help.
:help [command] print this summary or command-specific help
:completions <string> output completions for the given string
// truncated as the list is quite long!

  • :reset forgets any snippet evaluated so far. In other words, it restores the REPL to its initial state:
    scala> :reset
    Resetting interpreter state.
    Forgetting this session history:
    val a = “hello world”
    Forgetting all expression results and named terms: a
  •  :replay resets the REPL and executes all the commands in the session history:
    scala> :replay
    Replaying: val a = “hello world”
    val a: String = hello world
  •  :load <path> interprets snippets of code from a file. Suppose you have started the REPL from a directory containing a file called Test.scala and with text println (1 + 2). You can interpret the instructions in the Test.scala file as follows:
    scala> :load Test.scala
    Loading Test.scala…
    3

Include the relative and absolute path of your file to load documents from other folders of your machine. After reviewing the main REPL commands, let’s see how you can use it to interpret code fragments.

3. The REPL code evaluation

You are now ready to interpret Scala expressions in the REPL. Let’s start by evaluating the arithmetic expression 1 + 2:

scala> 1 + 2
val res0: Int = 3

Below figure analyzes the structure of your expression and its interpretation. It sums the integers 1 and 2 using the operator +, resulting in the value 3 of type Int. The REPL automatically saves the result as a value with a name having prefix res followed by an incremental number; in this case, it’s res0.

Analysis of the REPL’s evaluation of an arithmetic expression. The interpretation of the sum of two integers produces a constant of type Int. The REPL saves the result in a value called res0.

You can use the result value res0 in your computations. For example, you can do the following:

scala> res0 + 39
val res1: Int = 42

You can also request the result to be saved with a specific name:

scala> val x = 1 + 2
val x: Int = 3

scala> x
val x: Int = 3

Note that you have not specified any type explicitly in all the examples you have seen so far. Thanks to its type inference, the Scala compiler infers that summing two integers returns an integer. When needed, you can provide an explicit return type; if not compatible with the expression provided, the compiler will display an error.

The compiler knows how to convert an Int into a Double, but it cannot do the same when transforming an Int into a String.
You can define and call functions using the REPL. For example, you can implement a function sayHello that takes one parameter n of type String, and it returns a String.
Look at below figure for a diagram of how the REPL interprets a function.

Analysis of the REPL’s interpretation of a function definition. The REPL identifies the function by its name, parameter, and return type.

In the previous example, you didn’t have to specify the return type for the sayHello function; the compiler can correctly infer its type is String. When needed, you can specify the desired return type of a function, and the compiler will see if this is compatible with its implementation:

Let’s see how you can define a class and create an instance using the REPL:

Finally, you can implement and use singletons or objects as follows:

Scala Worksheet
Most IDEs with dedicated support for Scala, such as IntelliJ IDEA, often also support Scala Worksheets. These are files with extension sc that can contain snippets of Scala code. Your IDE will start a REPL session to evaluate each snippet of code within the file and display its results within the text editor itself. Scala Worksheets are a valid alternative to manually starting a Scala REPL session from the command line.

4. Other development tools

To complete the environment setup, you’ll also need to install other development tools: sbt, Git, and Docker. The next lesson will go into the details of how to install and use sbt. Let’s see how to set Git and Docker up in this section.

Git installation
Git is a commonly used open source version control system and is often pre installed in Unix machines. You’ll use it to download existing code and give you a head start in the more advanced lessons. Run the following command in the terminal to check if Git is already installed on your machine:

$ git — version
git version 2.21.1

If you need to install git, please follow the instructions for your operating system at https://github.com/git-guides/install-git.

Docker installation
Docker is a platform that helps you manage the complexity of your infrastructure using virtual machines. You’ll use Docker to run a temporary PostgreSQL database instance to use in your Scala applications. Follow the instructions on how to install Docker at https://docs.docker.com/get-docker. After its installation, you should be able to execute the docker — version command in your terminal and get an output similar to the following:

$ docker — version
Docker version 20.10.0, build 7287ab3

--

--

Pankaj Patil

Software Engineer | Blogger | Reader | Writer| Political and Social Activist