Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Setting Up Scala  ›  Lesson

The REPL: Answers in Seconds

Setting Up Scala 10 minThe REPL reads one expression at a time, evaluates it and prints its value with its type, which makes it the fastest place to test an idea before it goes into a file.

The REPL: Answers in Seconds

You want to know what 1530 / 5 gives in Scala before you trust it in the logbook. Writing a file, adding a @main function and running it is slow for a one-line question. The REPL answers it in a second.

The rule

Type an expression, get its value and its type back.

REPL stands for read, evaluate, print, loop. Start it by running scala with no file, then type at the scala> prompt:

scala> 5.0 + 8.5
val res0: Double = 13.5

Each answer has three parts:

  • res0 is a name the REPL invents for the result, so you can use it again: typing res0 * 2 next gives val res1: Double = 27.0.
  • Double is the type the compiler inferred.
  • 13.5 is the value.

Give things your own names and they stay available for the rest of the session. Continuing the same session:

scala> val runs = List(5.0, 8.5, 3.2, 10.0)
val runs: List[Double] = List(5.0, 8.5, 3.2, 10.0)

scala> runs.sum
val res2: Double = 26.7

A few commands start with a colon and talk to the REPL itself rather than to Scala:

  • :type runs.max prints just the type, Double.
  • :reset forgets everything you defined.
  • :help lists the commands, and :quit leaves.

Code that only prints returns Unit, and the REPL shows just the printed text: println("Logged") displays Logged with no res line.

If a line is unfinished, such as an if with no branches yet, the REPL waits for more lines before it evaluates anything.

The shortcut

Use the REPL as a question desk: one question per line, and read the type as well as the value. The type often answers a second question you did not know to ask.

Checking Asha's pace before coding itStep 1 of 4
  1. Seconds per kilometre

    1530 / 5.0 returns val res0: Double = 306.0. Her 5 km took 1530 seconds, so each kilometre took 306.

The REPL is for experiments. Anything the club will run again belongs in a .scala file, because a REPL session disappears when you quit.

Drills

Answer fast — trust the rule.

quick drill

In a fresh REPL you type 5.0 + 8.5. What is shown?

quick drill

Which input shows only the type of runs.max?

quick drill

Right after val res0: Double = 13.5, you type res0 * 2. What comes back?

quick drill

What does the REPL display for println("Logged")?

quick drill

Which job is the REPL the wrong tool for?

Remember

  • Run scala with no file to start the REPL; each result shows a name, a type and a value.
  • Shortcut: one question per line, and read the type too.
  • Trap: leaving real work in a REPL session; move it into a file.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChecking a pace in the REPL

A run of 5 km took 1530 seconds. Explain how you would use the REPL to work out the pace in minutes and seconds per kilometre, what you would type, and what the type shown in each answer tells you.