Configure and use Scala

Set the Scala version used for building the project

The scalaVersion configures the version of Scala used for compilation.
By default, sbt also adds a dependency on the Scala library with this
version. See the next section for how to disable this automatic
dependency. If the Scala version is not specified, the version sbt was
built against is used. It is recommended to explicitly specify the
version of Scala.

For example, to set the Scala version to “2.11.1”,

scalaVersion := "2.11.1"

Disable the automatic dependency on the Scala library

sbt adds a dependency on the Scala standard library by default. To
disable this behavior, set the autoScalaLibrary setting to false.

autoScalaLibrary := false

Temporarily switch to a different Scala version

To set the Scala version in all scopes to a specific value, use the ++
command. For example, to temporarily use Scala 2.10.4, run:

> ++ 2.10.4

Use a local Scala installation for building a project

Defining the scalaHome setting with the path to the Scala home
directory will use that Scala installation. sbt still requires
scalaVersion to be set when a local Scala version is used. For
example,

Build a project against multiple Scala versions

Enter the Scala REPL with a project’s dependencies on the classpath, but not the compiled project classes

The consoleQuick action retrieves dependencies and puts them on the
classpath of the Scala REPL. The project’s sources are not compiled, but
sources of any source dependencies are compiled. To enter the REPL with
test dependencies on the classpath but without compiling test sources,
run test:consoleQuick. This will force compilation of main sources.

Enter the Scala REPL with a project’s dependencies and compiled code on the classpath

The console action retrieves dependencies and compiles sources and
puts them on the classpath of the Scala REPL. To enter the REPL with
test dependencies and compiled test sources on the classpath, run
test:console.

Enter the Scala REPL with plugins and the build definition on the classpath

Define the commands evaluated when exiting the Scala REPL

Set cleanupCommands in console to set the statements to evaluate after
exiting the Scala REPL started by console and consoleQuick. To
configure consoleQuick separately, use
cleanupCommands in consoleQuick. For example,

Use the Scala REPL from project code

sbt runs tests in the same JVM as sbt itself and Scala classes are not
in the same class loader as the application classes. This is also the
case in console and when run is not forked. Therefore, when using
the Scala interpreter, it is important to set it up properly to avoid an
error message like:

Failed to initialize compiler: class scala.runtime.VolatileBooleanRef not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.

The key is to initialize the Settings for the interpreter using
embeddedDefaults. For example:

Here, MyType is a representative class that should be included on the
interpreter’s classpath and in its application class loader. For more
background, see the
original proposal that resulted in
embeddedDefaults being added.

Similarly, use a representative class as the type argument when using
the break and breakIf methods of ILoop, as in the following
example: