Learn Scala 3 Fast: The Scala REPL

Beginning: The Scala REPL

Okay, let’s start writing some code!

Assuming you have Scala-CLI installed on your computer, this lesson shows how to start something known as a REPL so you can start writing code. Remember that if you don’t have Scala installed on your computer, you can also use the Scastie website.

The REPL

The acronym REPL stands for “read/evaluate/print/loop,” and it’s an interactive tool that lets you write Scala code. I often refer to it as a playground or laboratory, because it’s a place where you can run experiments on Scala code to make sure it works like you expect it to. Or if you’re not familiar with a language feature or library, the REPL is a place where you can experiment with it.

If you’re using Scala-CLI, start the Scala REPL like this from your operating system command line:

$ scala-cli repl

Or, if you have the Scala 3 SDK installed on your system, start the REPL like this:

$ scala

In either case you should see a result that looks like this:

Welcome to Scala 3.1.1
Type in expressions for evaluation. Or try :help.

scala> _

The scala> prompt indicates that you’re now inside an interactive REPL session. In here you can experiment with writing Scala code:

scala> val x = 1
x: Int = 1

scala> val y = 2
y: Int = 2

scala> x + y
res0: Int = 3

As shown in these examples:

  • You generally create new variables in Scala with the val keyword. (You’ll see more on this in the lessons that follow.)
  • x and y are the names of two variables that I created.
  • After you enter your code and press the [Enter] key, the REPL output shows the result of your expression, including the variable name you gave it, its data type (such as Int), and its value.
  • If you don’t assign a variable name, as in the third example, the REPL creates its own variable, beginning with the name res0, then res1, etc. You can then use these variable names just as though you had created them yourself:
scala> res0 * 3
res1: Int = 9

This is how the REPL works: type your expressions, and see their results. This is why I refer to this as a playground, or a place to experiment.

Tab completion

One “trick” in the REPL is that you can type a value or the name of a variable, then type a decimal, and then press the [Tab] key. The REPL responds by showing all of the methods that are available on your value, such as when you follow those steps on an integer like the number 1:

scala> 1.[Tab]
!=                         finalize                   round
##                         floatValue                 self
%                          floor                      shortValue
&                          formatted                  sign
*                          getClass                   signum
many more methods listed here ...

What happens here is that 1 is an instance of the Scala type Int, which is an integer, and all of these methods are available on any integer. For instance, you can continue to type abs after the decimal to get the absolute value of an integer:

scala> 1.abs
val res1: Int = 1

Two REPL tips

At this point there are two other things to know about the REPL. First, you reset the REPL environment with its :reset command:

scala> :reset
Resetting REPL state.

This tells the REPL to forget everything you previously typed in, and to restore itself to its initial state.

Second, you quit a REPL session with the :quit command, or by typing the [Control][d] keystroke. Either of these ends your REPL session and returns you to your operating system command line.

Scastie, an online REPL

Remember that if you haven’t installed Scala-CLI or the Scala 3 SDK on your system, you can also use the Scastie website as an online REPL.

Scastie is a tool that’s created and maintained by the creators of Scala 3, and it lets you run Scala code in your browser. You just enter your code, press Run, and see the output.

I’ve worked at command line prompts for many years, so I prefer the REPL, but Scastie is also nice. Until we start writing scripts later in the book, either tool is fine.

Start using the REPL

If you haven’t used a REPL environment before, I highly recommend experimenting with it now. Even experienced Scala developers often have a REPL session open while they’re coding.

For example, type these expressions into the REPL to see their results:

val a = 2
val b = 4
val c = a * b
val d = c / 2
val e = d - 1
d == 3
d == 7

Those are all examples of how to work with integers, which have the type Int in Scala. Similarly, this example shows how to create a String and then get its length:

val s = "Hello, world"
s.length

Let the experimenting begin!

Exercises

The exercises for this lesson are available here.

books by alvin