Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
The REPL (Read-Eval-Print Loop) is a command-line tool provided by Scala, allowing developers to execute Scala code interactively. REPL is particularly useful for quick experimentation, learning Scala, or debugging. Every Scala expression you type into the REPL is read, evaluated, and the result is printed back to you.
You can start the REPL simply by running the scala
command in your terminal if you have Scala installed.
$ scala
Once inside, you'll see a prompt where you can start typing Scala expressions:
scala>
Simple Expressions: You can evaluate simple arithmetic or string operations.
scala> 2 + 3 val res0: Int = 5 scala> "Hello " + "World" val res1: String = "Hello World"
Defining Variables and Functions: The REPL allows you to define variables and functions which can be used later.
scala> val x = 10 val x: Int = 10 scala> def square(num: Int) = num * num def square(num: Int): Int scala> square(x) val res2: Int = 100
Importing Libraries: You can import libraries and use them directly.
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val arr = ArrayBuffer(1, 2, 3) val arr: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
Multiline Expressions: For multiline expressions like class definitions or longer functions, simply hit Enter
after each line.
scala> class Person(name: String, age: Int) { def greet() = s"Hello, my name is $name and I am $age years old." } defined class Person scala> val p = new Person("Alice", 30) val p: Person = Person@4a574795 scala> p.greet() val res3: String = "Hello, my name is Alice and I am 30 years old."
Exiting the REPL: You can exit the REPL by typing :quit
or :q
or by pressing Ctrl+D
.
scala> :quit
Tab
, it will attempt to complete the name for you or show you possible matches.:type <expression>
, you can see the type of an expression. Using :doc <expression>
(available in newer Scala versions) provides the documentation of an expression.:reset
to reset the REPL environment.The Scala REPL is a powerful tool for interactive programming, learning, and debugging in Scala. It provides immediate feedback, allowing you to quickly test ideas, explore libraries, or practice the language.
Interacting with Scala Code in REPL:
val greeting = "Hello, Scala!"
Loading External Files in Scala REPL:
:load
or :l
command to load external Scala files.// In REPL :load path/to/external/file.scala
Using Libraries and Dependencies in Scala REPL:
:require
or :jar
commands.// In REPL :require <groupId> %% <artifactId> % <version>
REPL Features in Scala 3:
// In Scala 3 REPL val result = 2 + 2
Scala REPL for Testing and Prototyping:
// In REPL val list = List(1, 2, 3) list.map(_ * 2)
Tips and Tricks for Effective Use of Scala REPL:
:paste
mode for multi-line code.// In REPL :paste // Multi-line code