Skip to Content
Course content

4: Using the Scala REPL

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, why bother with the REPL if I can just write a .scala file?

I've seen a lot of developers ignore the REPL because they're used to the "write-compile-run" cycle of Java or C#. But in Scala, the REPL (Read-Eval-Print Loop) is your best friend for rapid prototyping. Think of it as a scratchpad. If I'm not 100% sure how a specific collection method like groupBy or foldLeft behaves with a weird edge case, I'm not going to spin up a whole project, create a main method, and wait for a build tool to compile everything just to test one line of code.

For example, if I want to see exactly what happens when I filter a list of strings based on a regex, I just pop open the REPL and go:

scala> val emails = List("test@example.com", "invalid-email", "dev@scala.org")
scala> emails.filter(_.contains("@"))
res2: List[String] = List(test@example.com, dev@scala.org)

It's instant. I get a result immediately without the boilerplate. Once I know the logic works, then I move it into my actual source code.

Can I define actual functions and classes in here, or is it just for one-liners?

You can absolutely define full structures. The REPL isn't just a calculator; it's a fully functional Scala environment. I often use it to sketch out case classes to see if my data model makes sense before I commit it to a file. When you define something in the REPL, it stays in the session's memory until you exit.

Check this out:

scala> case class User(name: String, age: Int)
scala> def isAdult(u: User): Boolean = u.age >= 18
scala> val bob = User("Bob", 20)
scala> isAdult(bob)
res5: Boolean = true

I just defined a data model and a piece of business logic in seconds. I personally find this invaluable when I'm learning a new library—I can import a class, instantiate it, and call methods on it to see what's actually happening under the hood without guessing based on the documentation.

How do I handle multi-line blocks without it executing too early?

This is usually where people get frustrated. If you hit enter on a line that isn't "complete" (like an open curly brace), the REPL is smart enough to know you aren't finished. It will change the prompt from scala> to *, indicating it's waiting for you to close the block.

If you're writing a slightly longer function, just keep typing:

scala> def greet(name: String) = {
*   val message = s"Hello, $name!"
*   println(message)
* }
scala> greet("Alice")
Hello, Alice!
res8: Unit = ()

But here is a pro tip: if you have a large chunk of code copied from a document or a website, typing it manually is a pain and prone to errors. Use the :paste command. Type :paste, hit enter, paste your entire block of code, and then hit Ctrl+D (on Unix) or Ctrl+Z (on Windows) to tell the REPL you're done. It'll evaluate the whole block at once. It saves me a ton of time when I'm sharing snippets with teammates.




📋 Practical Task

Validating a Domain-Specific String Parser in the REPL

Imagine you are building a system that processes product SKUs. The rule is that a valid SKU must start with "PROD-", be exactly 12 characters long, and end with a number.

Instead of writing a full application, use your Scala REPL to prototype the validation logic. Complete the following steps:

  • Start your Scala REPL.
  • Define a case class Product(sku: String).
  • Define a function def isValid(p: Product): Boolean that implements the three rules mentioned above (starts with "PROD-", length of 12, ends with a digit).
  • Test your function with at least three different inputs: one that is perfectly valid, one that is too short, and one that doesn't end in a number.

Once you have the isValid function returning the correct booleans for all three test cases, you've successfully used the REPL to verify your logic!

Rating
0 0

There are no comments for now.

to be the first to leave a comment.