Scala cheat sheet

A scannable Scala reference: 8 short snippets across 5 topics, each linking back to the lesson it came from.

At a glance

TopicWhat it covers
Setting up Scala 3: Coursier, Scala CLI and sbtCoursier is the installer that the Scala ecosystem itself recommends. It fetches the JVM, Scala CLI, sbt and scalafmtlesson
Option, Either and functional error handlingExceptions are for the edges: I/O, a broken invariant, a bug. Model the outcomes you expect your caller to handle aslesson
Givens, implicits and type classesPutting the instances in the companion object means they are found through implicit scope, so a caller needs no importlesson
Testing with ScalaTest and MUnitStructure suites and assertions, use property-based testing with ScalaCheck, and run the right tests from sbt and Scalalesson
sbt, dependencies and project structureKeep the dependency direction acyclic and pointing inward: the domain module knows nothing about the HTTP module. sbtlesson

Quick snippets

Setting up Scala 3: Coursier, Scala CLI and sbt

Coursier and the toolchain

# install Coursier, then the tools
curl -fL https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs && ./cs setup          # adds Coursier to PATH and installs the basics

cs install scala-cli sbt scalafmt
cs java --jvm temurin:21           # install and print the JAVA_HOME of a JDK
eval "$(cs java --env --jvm 21)"   # put that JDK on PATH for this shell

scala-cli version
sbt --version
scala --version

Coursier and the toolchain

//> using scala 3.5.0
//> using dep com.lihaoyi::os-lib:0.10.7
//> using test.dep org.scalameta::munit:1.0.0

import java.nio.file.Files

@main def demo(): Unit =
  val files = os.list(os.pwd).take(5)
  println(files.mkString(", "))

Coursier and the toolchain

scala-cli run demo.scala
scala-cli test .
scala-cli package . -o app --assembly     # a runnable fat jar
scala-cli repl

Full lesson: Setting up Scala 3: Coursier, Scala CLI and sbt →

Option, Either and functional error handling

Either for failure with a reason

import scala.util.{Try, Success, Failure}

// wrap a throwing call once, at the boundary
val parsed: Try[Int] = Try("not a number".toInt)

parsed match
  case Success(v) => println(v)
  case Failure(e) => println(s"failed: ${e.getMessage}")

// Either and Try agree on for-comprehensions, so they compose
def readPort(s: String): Either[String, Int] =
  Try(s.toInt).toEither.left.map(_ => s"'$s' is not a number").filterOrElse(_ > 0, "port must be positive")

Full lesson: Option, Either and functional error handling →

Givens, implicits and type classes

A type class end to end

// overriding an instance in a specific scope, for a test or a special case
object TestScope:
  given testEncoder: Encoder[Int] with
    def encode(a: Int) = "REDACTED"

  def render: String =
    import Encoder.encode
    encode(42)                      // picks the local given, which wins over the companion

Full lesson: Givens, implicits and type classes →

Testing with ScalaTest and MUnit

Property-based testing with ScalaCheck

// run inside sbt
//   testOnly com.example.CodecProperties
//   testOnly * -- -z "decode"
// Scala CLI
//   scala-cli test .
//   scala-cli test . --test-only com.example.CodecProperties

Full lesson: Testing with ScalaTest and MUnit →

sbt, dependencies and project structure

Cross-building and publishing

// cross-build for both Scala lines
ThisBuild / crossScalaVersions := Seq("3.5.0", "2.13.14")

// publish to a company repository
publishTo := Some("Company" at "https://repo.example.com/maven-releases")
credentials += Credentials(Path.userHome / ".sbt" / ".credentials")

// a library that must not ship test sources
Compile / packageBin / packageOptions +=
  Package.ManifestAttributes("Implementation-Version" -> version.value)

Cross-building and publishing

sbt +compile          # compile for every crossScalaVersions entry
sbt +publishLocal     # install into the local Ivy repository
sbt evicted           # show which dependency version won and why
sbt dependencyTree    # the full resolution graph
sbt ";clean;test"     # several commands in one invocation

Full lesson: sbt, dependencies and project structure →

FAQ

Is this Scala cheat sheet free to use?
Yes. No sign-up and no tracking: the page is static, every example is on the page itself, and you can print it or save it as a one-page reference.
Where do the examples come from?
Every snippet is taken from the 5 lessons of the Scala course on this site, and each section links back to the lesson it was pulled from.
How do I go deeper than a cheat sheet?
Open the full Scala course — it carries the worked explanations, the edge cases and the exercises behind every line here.

C C++ C# Lua Dart

Last refreshed 2026-09-27.