Using the REPL

Quite a few languages provide a REPL—read-eval-print loop—a tool that’s a convenient way to key in snippets of code and interactively see the code come to life immediately. In addition to executing code snippets, REPLs often provide details that are not easily accessible at runtime. That makes REPL a special tool for experimentation and also to learn how the language infers types for variables and functions.

The command-line shell named scala is the REPL for Scala and it’s the quickest way to try out the language. Using this tool we can start playing with small code snippets. This is not simply a learning tool; it comes in handy during development of large applications as well. You can quickly try out some code ideas—micro prototyping—in the REPL and then use the world’s best technology of copy and paste to get the code from the REPL into your application.

To start the REPL, on the command line (in a terminal window or command prompt), type scala. That should print an introductory message followed by a prompt:

 
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java
 
1.8.0_45).
 
Type in expressions to have them evaluated.
 
Type :help for more information.
 
 
scala>

At the prompt, type val number = 6, and hit Return. The Scala shell responds to indicate that it inferred the variable number to be an Int based on what’s assigned to it, the value 6:

 
scala> val number = 6
 
number: Int = 6

Now try reassigning number, and Scala will respond with this error:

 
scala> number = 7
 
<console>:11: error: reassignment to val
 
number = 7
 
^

Scala complains that we can’t reassign the constant number. In the console, however, we can redefine constants and variables. For example, the shell will quietly accept the following:

 
scala> val number = 7
 
number: Int = 7

Redefining constants and variables within the same scope is possible only in the interactive shell, and not in real Scala code or script—this flexibility makes it easier to experiment within the shell and, at the same time, prevents errors in application code.

We saw how Scala inferred the type as Int. Let’s try another example where it infers a variable as List.

 
scala> val list = List(1, 2, 3)
 
list: List[Int] = List(1, 2, 3)

Any time while writing application code, if you’re not sure what an expression will be inferred as, quickly try it in the shell.

In the shell, use the up arrow to bring back commands typed previously. The shell can even bring back commands from a previous invocation of the shell. While typing a line of command, press Ctrl+A to go to the beginning of the line or Ctrl+E to go to the end of the line.

The shell tries to execute what’s typed as soon it receives the Return key. If we type something incomplete and press Return, for example in the middle of writing a method definition, the shell will prompt for more code with a vertical bar (|). For example, let’s define a method isPalindrome on two lines, then call the method twice and view the results:

 
scala> def isPalindrome(str: String) =
 
| str == str.reverse
 
isPalindrome: (str: String)Boolean
 
 
scala> isPalindrome("mom")
 
res0: Boolean = true
 
 
scala> isPalindrome("dude")
 
res1: Boolean = false
 
 
scala> :quit

Type :quit to exit the shell.

Rather than keying in all the code, we can load code from a file into the shell using the :load option. For example, to load a file named script.scala, type :load script.scala. This option is useful to load and experiment with prewritten functions and classes while remaining in the interactive mode.

Although the shell is a convenient way to experiment with small pieces of code, you’ll soon want to find a way to easily run code saved in files—you’ll learn how to do that next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.181.87