Skip to content

Instantly share code, notes, and snippets.

@trane
Created August 19, 2017 15:20
Show Gist options
  • Save trane/e25c030185a90c2a8accd12fce1930c5 to your computer and use it in GitHub Desktop.
Save trane/e25c030185a90c2a8accd12fce1930c5 to your computer and use it in GitHub Desktop.
A quick-start REPL intro

Scala REPL (Read Eval Print Loop)

You can get into a REPL on (OS X or Linux) from your terminal, from two different commands.

If you have scala installed, you can type:

$ scala

Or, more likely, you have sbt installed:

$ sbt
> console

You can also get to the scala console from your IDE.

Tips

The REPL allows you to quickly iterate on ideas you have without having to make a completely valid .scala file.

Experimentation

Try typing into the REPL, here are some examples:

scala> 1+1
res0: Int = 2

scala> "hello"
res1: String = hello

scala> 1 / 2.0
res2: Double = 0.5

scala> 1 / 2.1
res3: Double = 0.47619047619047616

If you give the REPL a command it can't understand, it will tell you:

scala> "hihi
<console>:1: error: unclosed string literal
       "hihi
       ^

Help

The REPL comes with a help guide for whenever you have a question about what it can do:

scala> :help
All commands can be abbreviated, e.g., :he instead of :help.
:edit <id>|<line>        edit history
:help [command]          print this summary or command-specific help
:history [num]           show the history (optional num is commands to show)
:h? <string>             search the history
:imports [name name ...] show import history, identifying sources of names
:implicits [-v]          show the implicits in scope
:javap <path|class>      disassemble a file or class name
:line <id>|<line>        place line(s) at the end of history
:load <path>             interpret lines in a file
:paste [-raw] [path]     enter paste mode or paste a file
:power                   enable power user mode
:quit                    exit the interpreter
:replay [options]        reset the repl and replay all previous commands
:require <path>          add a jar to the classpath
:reset [options]         reset the repl to its initial state, forgetting all session entries
:save <path>             save replayable session to a file
:sh <command line>       run a shell command (result is implicitly => List[String])
:settings <options>      update compiler options, if possible; see reset
:silent                  disable/enable automatic printing of results
:type [-v] <expr>        display the type of an expression without evaluating it
:kind [-v] <type>        display the kind of a type. see also :help kind
:warnings                show the suppressed warnings from the most recent line which had any

History

Look back at the commands you have given:

scala> :history
2507  1+1
2508  "hello"
2509  1 / 2.0
2510  1 / 2.1
2511  "hihi
2512  :help
2513  :history

Save

You can use :save to write your REPL session to your current directory

scala> :save repl.sc

Now repl.sc contains:

cat repl.sc 
1+1
"hello"
1 / 2.0
1 / 2.1

Note that the statements saved from the REPL might not be valid .scala, which is why I'm using the .sc extension. You could use any extension you would like.

Load the REPL saved file

scala> :load repl.sc
Loading repl.sc...
res4: Int = 2
res5: String = hello
res6: Double = 0.5
res7: Double = 0.47619047619047616

Copy and Paste a Session

You can copy and paste the entire block of text from the REPL (even though it's not valid syntax) because the REPL is smart enough to know what you are doing:

I have copied all of the commands I have run so far and pasted them (I then held ctrl and then d to finish pasting)

scala> scala> 1+1

// Detected repl transcript. Paste more, or ctrl-D to finish.

res0: Int = 2

scala> "hello"
res1: String = hello

scala> 1 / 2.0
res2: Double = 0.5

scala> 1 / 2.1
res3: Double = 0.47619047619047616

scala> "hihi

// Replaying 5 commands from transcript.

scala> 1+1
res8: Int = 2

scala> "hello"
res9: String = hello

scala> 1 / 2.0
res10: Double = 0.5

scala> 1 / 2.1
res11: Double = 0.47619047619047616

scala> "hihi
<console>:1: error: unclosed string literal
       "hihi
       ^

Copy and Paste a file

Let's say you have a file Foo.scala that has the following code in it:

case class Foo(bar: String)
object Foo { def foo: Foo = Foo("Hello") }

If you try to copy-and-paste these lines into the REPL it will give you an error, since the case class and object definitions won't be treated as companions. That's where the :paste command comes in:

scala> :paste
// Entering paste mode (ctrl-D to finish)

case class Foo(i: Int)
object Foo { def empty: Foo = Foo(0) }

// Exiting paste mode, now interpreting.

defined class Foo
defined object Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment