Getting Started with Scala

November 10th, 2023

Scala is a functional programming language which can generate JVM bytecode and interop with Java and any other language which is able to output jarfiles. The language has a high barrier to entry to due to it's unique build system and Scala programmer's propensity to use some of the language's more advanced features. However, it's actually pretty simple to get started with a development environment. You will need two dependencies installed:

  1. Java
  2. sbt

The "compilcated build system" mentioned earlier is called SBT (simple build tool/Scala build tool). You can get your own sbt by installing coursier, which will set up your system with sbt, scala (the Scala REPL), Java, and some other goodies which can make developing and running Scala code more ergonomic.

Many developers who work on the JVM (including myself) would recommend using a package manager to manage your installed Java versions. I use sdkman to do this. You you're familiar with nvm, then you will be able to get used to sdkman quite quickly.

Once you have java and sbt on your path, you can start writing Scala! Clone a Scala project, run sbt, and voila! You're all ready to go. Once in the sbt shell, you can use the build system to compile, test, and run your code. Although you can also run all these commands from your shell as subcommands to sbt, such as sbt compile, it is much quicker to use the sbt shell since the project's configuration is already loaded.

If your project has multiple modules, you can select which one you want to work on by using project <project name> or by using <project name>/<command>.

One nice feature about sbt is that it will automatically download any dependencies you need. For example, if you want to use a different scala version, you can just update the scala version in your build.sbt file and sbt will download the new compiler and standard library for you. This is also true for any other dependencies you add to your project.

Hello, World!

The example below shows how to setup a "Hello, World!" project in Scala. This project will be a single Scala source file, along with a single build.sbt source.


// src/main/scala/net/davidwiles/HelloWorld.scala
@main def main(): Unit = {
  println("Hello, World!")
}
  

// build.sbt
lazy val root = (project in file("."))
  .settings(
    name := "hello-world",
    organization := "net.davidwiles",
    version := "0.1.0",
    scalaVersion := "3.3.1",
  )
  

// project/build.properties
sbt.version = 1.9.6
  

$ sbt
[info] welcome to sbt 1.9.6 (Oracle Corporation Java 17.0.1)
[info] loading global plugins from
[info] loading project definition from
[info] loading settings for project hello-world from build.sbt ...
[info] set current project to root
[info] sbt server started at
[info] set current project to hello-world (in build file:/home/david/Projects/hello-world/)
[info] started sbt server
sbt:root> run
[info] running net.davidwiles.main
Hello, World!
[success] Total time: 5 s, completed Nov 10, 2023, 5:58:47PM