14.1. Getting Started with the Scala REPL
You want to get started using the Scala REPL, including understanding some of its basic features, such as tab completion, starting the REPL with different options, and dealing with errors.
To start the Scala REPL, type
scala at your operating system command
You’ll see a welcome message and Scala prompt:
Welcome to Scala version 2.10.0 Type in expressions to have them evaluated. Type :help for more information. scala> _
Welcome, you’re now using the Scala REPL.
Inside the REPL environment, you can try all sorts of different experiments and expressions:
val x, y = 1x: Int = 1 y: Int = 1 scala>
x + yres0: Int = 2 scala>
val a = Array(1, 2, 3)a: Array[Int] = Array(1, 2, 3) scala>
a.sumres1: Int = 6
As shown in the second example, if you don’t assign the result of
an expression to a variable, the REPL creates its own variable,
res1, etc. You can use these variable names
just as though you had created them yourself:
res1.getClassres2: Class[Int] = int
Writing tests like this in the REPL is a great way to run experiments outside of your IDE or editor.
There are a few simple tricks that can make using the REPL more
effective. One trick is to use tab completion to
see the methods that are available on an object. To see how tab
completion works, create a
String object, type a decimal, and then press the Tab key. With Scala 2.10, the REPL shows that more than 30 methods are available on ...