Scala REPL Basics

The REPL is your fastest way to experiment with Scala. REPL stands for Read-Eval-Print Loop. You type a Scala expression, press Enter, and the REPL reads it, evaluates it, prints the result, and waits for the next input. No files, no compilation steps — just instant feedback.

What a REPL Looks Like


You Type          →   REPL Evaluates   →   REPL Prints Result
─────────────────────────────────────────────────────────────
2 + 3                   5                    val res0: Int = 5
"hello".length         5                    val res1: Int = 5
"hi" + " there"    "hi there"           val res2: String = hi there

Think of the REPL as a conversation with Scala. You ask a question (type an expression), Scala gives an immediate answer (prints the result). It is the best tool for testing small ideas before putting them into a full program.

Starting the REPL

Open your terminal and type:

scala

You see a prompt like this:

Welcome to Scala 3.x.x
Type in expressions for evaluation. Or try :help.

scala>

The scala> prompt means the REPL is ready for input. Everything you type after this prompt runs as Scala code.

Basic Arithmetic in the REPL

scala> 10 + 5
val res0: Int = 15

scala> 100 - 37
val res1: Int = 63

scala> 6 * 7
val res2: Int = 42

scala> 20 / 4
val res3: Int = 5

scala> 17 % 5
val res4: Int = 2

The REPL names each result automatically: res0, res1, and so on. You can use these names in later expressions:

scala> res0 + res2
val res5: Int = 57

Declaring Values and Variables

scala> val city = "Mumbai"
val city: String = Mumbai

scala> var score = 100
var score: Int = 100

scala> score = 150
// score: Int = 150

scala> city = "Delhi"
-- Error: Reassignment to val

val is permanent. Once set, you cannot change it — just like writing in ink. var is flexible — you can overwrite it, like writing in pencil. Scala prefers val because immutable values are easier to trust and test.

String Operations

scala> val name = "Scala"
val name: String = Scala

scala> name.length
val res6: Int = 5

scala> name.toUpperCase
val res7: String = SCALA

scala> name.toLowerCase
val res8: String = scala

scala> name.reverse
val res9: String = alacS

scala> name.contains("ca")
val res10: Boolean = true

scala> name.startsWith("Sc")
val res11: Boolean = true

In Scala, strings are objects with built-in methods. You call methods with a dot followed by the method name. This is object-oriented syntax in action.

Multi-line Input

You can write a function directly in the REPL. The REPL recognizes that your expression is incomplete and shows | to let you continue:

scala> def double(x: Int): Int =
     |   x * 2
def double(x: Int): Int

scala> double(7)
val res12: Int = 14

REPL Command Reference


:help      → show all available REPL commands
:quit      → exit the REPL (or press Ctrl+D)
:reset     → clear all defined values and start fresh
:paste     → enter paste mode (for multi-line code blocks)
:type      → show the type of an expression without running it
:imports   → list all current imports

Example:
scala> :type 42 + 1.0
Double

Paste Mode for Multi-line Code

Paste mode lets you paste a full block of code at once without the REPL trying to run each line separately:

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

def greet(name: String): String =
  "Hello, " + name + "!"

// Exiting paste mode, now interpreting.
def greet(name: String): String

scala> greet("World")
val res13: String = Hello, World!

Checking Types with :type

You can ask the REPL what type an expression produces without actually evaluating it:

scala> :type 3.14
Double

scala> :type "hello"
String

scala> :type List(1, 2, 3)
List[Int]

scala> :type (x: Int) => x * 2
Int => Int

This is very useful when you want to understand how Scala infers types without running code that might have side effects.

Importing Libraries in the REPL

You can import Scala's standard library directly in the REPL:

scala> import scala.math._

scala> sqrt(16.0)
val res14: Double = 4.0

scala> pow(2, 10)
val res15: Double = 1024.0

scala> Pi
val res16: Double = 3.141592653589793

The underscore _ in scala.math._ means "import everything from scala.math." It is Scala's equivalent of import * in Python.

Using the REPL as a Calculator

The REPL works perfectly as an advanced calculator:

scala> val principal = 10000.0
scala> val rate = 0.05
scala> val years = 3
scala> val interest = principal * rate * years
val interest: Double = 1500.0

scala> val total = principal + interest
val total: Double = 11500.0

REPL vs Writing Files


REPL                          .scala Files
──────────────────────────    ──────────────────────────
Immediate feedback            Must compile first
Good for experiments          Good for real programs
No file needed                Saved to disk
Results lost on exit          Code is permanent
Great for learning            Great for production

Common Beginner Mistakes in the REPL

Forgetting to Press Enter Twice for Block End

Sometimes the REPL waits for more input. If you see | and you are done typing, press Enter once to submit. If it still waits, check for an unclosed parenthesis or bracket.

Reassigning a val

The REPL gives a clear error when you try to change a val. Switch to var if you need a mutable value, or define a new val with a different name.

Division of Integers

scala> 7 / 2
val res17: Int = 3    // NOT 3.5!

When both numbers are integers, Scala performs integer division. To get a decimal result, make at least one number a decimal:

scala> 7.0 / 2
val res18: Double = 3.5

Practice in the REPL

Open your REPL and try these exercises to build confidence:

Calculate how many seconds are in a week: 7 * 24 * 60 * 60

Check if "Functional" contains "Fun": "Functional".contains("Fun")

Create a function that squares a number and call it three times with different inputs.

The REPL is a sandbox where mistakes cost nothing. Use it freely to explore Scala before writing full programs.

Leave a Comment

Your email address will not be published. Required fields are marked *