The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code.
You start a REPL session by running the scala
or scala3
command depending on your installation at your operating system command line, where you’ll see a “welcome” prompt like this:
$ scala
Welcome to Scala 2.13.15 (OpenJDK 64-Bit Server VM, Java 1.8.0_342).
Type in expressions for evaluation. Or try :help.
scala> _
$ scala
Welcome to Scala 3.5.2 (1.8.0_322, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> _
The REPL is a command-line interpreter, so it sits there waiting for you to type something. Now you can type Scala expressions to see how they work:
scala> 1 + 1
val res0: Int = 2
scala> 2 + 2
val res1: Int = 4
As shown in the output, if you don’t assign a variable to the result of an expression, the REPL creates variables named res0
, res1
, etc., for you.
You can use these variable names in subsequent expressions:
scala> val x = res0 * 10
val x: Int = 20
Notice that the REPL output also shows the result of your expressions.
You can run all sorts of experiments in the REPL.
This example shows how to create and then call a sum
method:
scala> def sum(a: Int, b: Int): Int = a + b
def sum(a: Int, b: Int): Int
scala> sum(2, 2)
val res2: Int = 4
If you prefer a browser-based playground environment, you can also use scastie.scala-lang.org.
If you prefer writing your code in a text editor instead of in console prompt, you can use a worksheet.
Contributors to this page:
Contents
- Introduction
- Scala Features
- Why Scala 3?
- A Taste of Scala
- Hello, World!
- The REPL
- Variables and Data Types
- Control Structures
- Domain Modeling
- Methods
- First-Class Functions
- Singleton Objects
- Collections
- Contextual Abstractions
- Toplevel Definitions
- Summary
- A First Look at Types
- String Interpolation
- Control Structures
- Domain Modeling
- Tools
- OOP Modeling
- FP Modeling
- Methods
- Method Features
- Main Methods in Scala 3
- Summary
- Functions
- Anonymous Functions
- Function Variables
- Eta-Expansion
- Higher-Order Functions
- Write Your Own map Method
- Creating a Method That Returns a Function
- Summary
- Packaging and Imports
- Scala Collections
- Collections Types
- Collections Methods
- Summary
- Functional Programming
- What is Functional Programming?
- Immutable Values
- Pure Functions
- Functions Are Values
- Functional Error Handling
- Summary
- Types and the Type System
- Inferred Types
- Generics
- Intersection Types
- Union Types
- Algebraic Data Types
- Variance
- Opaque Types
- Structural Types
- Dependent Function Types
- Other Types
- Contextual Abstractions
- Extension Methods
- Context Parameters
- Context Bounds
- Given Imports
- Type Classes
- Multiversal Equality
- Implicit Conversions
- Summary
- Concurrency
- Scala Tools
- Building and Testing Scala Projects with sbt
- Worksheets
- Interacting with Java
- Scala for Java Developers
- Scala for JavaScript Developers
- Scala for Python Developers
- Where To Go Next