Skip to content

Instantly share code, notes, and snippets.

@trane trane/
Created Aug 19, 2017

What would you like to do?
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.


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


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


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


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


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

scala> :save

Now contains:

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
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

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
You can’t perform that action at this time.