1. Introduction

REPL is an acronym for Read-Eval-Print-Loop that is available in most programming languages to quickly try small code samples. Scala also provides its version of REPL along with the language installation. Additionally, SBT also provides two slight variations of the REPL as console and consoleQuick.

In this quick tutorial, we’ll look at Scala REPL, SBT console, and SBT consoleQuick, and point out their differences.

2. Scala REPL

The Scala REPL is installed automatically along with the Scala compiler installation. We can start the REPL using the command:

scala

This brings up the Scala REPL, where we can execute Scala code:
Scala REPL
This is very helpful in performing basic Scala operations and checks. However, it becomes a bit cumbersome if we want to use external libraries as part of the sample code. It’s generally suggested to create a small project using a build tool such as SBT or Maven to try out library dependencies.

3. SBT REPLs

SBT provides two variations of its REPL: console and consoleQuick. The advantage of using the SBT REPLs is that they bring the necessary dependencies of the project into the REPL scope. As a result, we can access these library dependencies in the REPL code within SBT. Let’s look at each of these in more depth.

3.1. SBT console

We can start an SBT REPL session using the command:

sbt console

This loads the REPL:

SBT Console REPL

Apart from starting the REPL, it also adds all the main dependencies from the build to the classpath. We can directly use these libraries by importing them just like in any Scala files.

However, SBT compiles all the source files before starting the REPL session. As a result, if there’s a compilation error in any of the source files, the sbt console fails to start.

Moreover, sbt console doesn’t load test dependencies into the classpath. To include the test dependencies, we need to use the test scope:

sbt test:console

Now, we can import and use the test library dependencies in the REPL.

3.2. SBT consoleQuick

As mentioned in the previous section, sbt console starts up successfully only if all the source is compiled successfully. However, this is difficult, especially during major refactoring where there are compilation errors. This makes it impossible to try small code snippets during refactoring.

SBT provides another command where we can start the REPL with all the dependencies, but without compiling the source code in the project:

sbt consoleQuick

Now, we can execute sample Scala code using the dependencies from the build. Similarly to sbt console, we have to start this REPL in test scope if we want to use our test dependencies:

sbt test:consoleQuick

With this, we can use all the test libraries as well as the main dependencies within the REPL session. However, we should note that console and consoleQuick REPLs will not load if there are SBT project errors in the build configurations.

4. Conclusion

In this short article, we looked at the differences between REPLs provided with the Scala compiler and SBT. We also looked at using main and test dependencies within an SBT REPL session. Finally, we discussed how to use an SBT REPL even when there are compilation errors in the project.