Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Scala | REPL

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.

Starting the REPL:

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>

Using the REPL:

  1. 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"
    
  2. 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
    
  3. 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)
    
  4. 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."
    
  5. Exiting the REPL: You can exit the REPL by typing :quit or :q or by pressing Ctrl+D.

    scala> :quit
    

Tips:

  1. Tab Completion: The REPL supports tab completion. If you start typing a method or variable name and hit Tab, it will attempt to complete the name for you or show you possible matches.
  2. Viewing Documentation: By typing :type <expression>, you can see the type of an expression. Using :doc <expression> (available in newer Scala versions) provides the documentation of an expression.
  3. Resetting the REPL: If you need a clean slate, you can type :reset to reset the REPL environment.

Conclusion:

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.

  1. Interacting with Scala Code in REPL:

    • Enter Scala code directly in the REPL.
    • Code is evaluated immediately after pressing Enter.
    val greeting = "Hello, Scala!"
    
  2. Loading External Files in Scala REPL:

    • Use :load or :l command to load external Scala files.
    // In REPL
    :load path/to/external/file.scala
    
  3. Using Libraries and Dependencies in Scala REPL:

    • Add dependencies using :require or :jar commands.
    • Example using Ammonite's Ivy integration:
    // In REPL
    :require <groupId> %% <artifactId> % <version>
    
  4. REPL Features in Scala 3:

    • Scala 3 introduces new features in the REPL.
    • Enhanced syntax and improved user experience.
    // In Scala 3 REPL
    val result = 2 + 2
    
  5. Scala REPL for Testing and Prototyping:

    • Rapidly test and prototype code in the REPL.
    • Ideal for exploring language features and APIs.
    // In REPL
    val list = List(1, 2, 3)
    list.map(_ * 2)
    
  6. Tips and Tricks for Effective Use of Scala REPL:

    • Utilize the history with arrow keys.
    • Use :paste mode for multi-line code.
    • Explore implicit values and conversions interactively.
    // In REPL
    :paste
    // Multi-line code
    
    • Leverage auto-completion and syntax highlighting.