When you execute
scala in a terminal without additional parameters it opens up a REPL (Read-Eval-Print Loop) interpreter:
nford:~ $ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66). Type in expressions for evaluation. Or try :help. scala>
The REPL allows you to execute Scala in a worksheet fashion: the execution context is preserved and you can manually try out commands without having to build a whole program. For instance, by typing
val poem = "As halcyons we shall be" would look like this:
scala> val poem = "As halcyons we shall be" poem: String = As halcyons we shall be
Now we can print our
scala> print(poem) As halcyons we shall be
val is immutable and cannot be overwritten:
scala> poem = "Brooding on the open sea" <console>:12: error: reassignment to val poem = "Brooding on the open sea"
But in the REPL you can redefine a
val (which would cause an error in a normal Scala program, if it was done in the same scope):
scala> val poem = "Brooding on the open sea" poem: String = Brooding on the open sea
For the remainder of your REPL session this newly defined variable will shadow the previously defined variable. REPLs are useful for quickly seeing how objects or other code works. All of Scala's features are available: you can define functions, classes, methods, etc.