The Scala REPL
The Scala REPL is a chat window where you type one line and get an answer straight away. It is like a scratch pad for trying out ideas without writing a whole program.
In this page:
Syntax
$ scala
scala> expression
The Scala REPL
The Scala REPL (Read-Eval-Print Loop), started by typing scala with no arguments, lets you type expressions and see results immediately, each annotated with its inferred type.
It's the fastest way to experiment with small snippets before writing a full program. Every top-level expression you enter is automatically evaluated and printed.
Note:
Type
:quit (or press Ctrl+D) to exit the Scala REPL.Example: The Scala REPL
object Main extends App {
// Simulating what you'd see typing directly into the REPL:
val result = 2 + 2
println(s"2 + 2 = $result")
println("In the REPL, typing '2 + 2' alone would print 'val res0: Int = 4'")
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Typing a multi-line class into the REPL and pressing Enter after each line, when the REPL runs each input immediately unless you use
:paste. - Expecting REPL variables to persist after quitting, when they are lost after
:quit. - Confusing
scalafor the REPL withscala file.scalafor running a file, and being surprised by the REPL prompt.
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: