Skip to content

Scala is the place to practise functional programming with a type system strong enough to encode real constraints. On the JVM it inherits garbage collection and a vast library ecosystem; on top of that it adds immutable case classes, exhaustive pattern matching, higher-kinded types, and (in Scala 3) given instances for type-class derivation. The exercises in this chapter use those features for what they are good at: making illegal states unrepresentable, composing effects, and deriving code at compile time.

Concepts from other sections (5)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

  • Exhaustive pattern matching: a match that the compiler checks covers every variant of a type, so adding a variant turns each place that does not handle it into a compile error. Read more: Chapter 21: Rust
  • Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller in Mathematics
  • Algebraic data type (ADT): a type built from sums (“one of these variants”) and products (“all of these fields”), such as a Rust enum with payloads; with exhaustive matching, every case must be handled explicitly. Read more: Chapter 21: Rust
  • Checked exception: a Java exception that a method must declare in its throws clause and that callers must catch or declare in turn, unlike unchecked exceptions, which need neither. Read more: Chapter 26: Java
  • Lazy evaluation: deferring computation until its result is needed. Read more: 19.5 Expression templates

Note: Scala is an optional language here. None of the core books target it, so these exercises have no worked solutions yet and nothing in this chapter is built in CI. The briefs are kept because they are worth reading as design problems. To get a toolchain, run brew install scala-cli; on a chezmoi-managed machine it comes from the install_alt_langs flag, which is off by default. scala-cli runs a bare directory of sources and manages its own JVM, so no build definition is needed.

Learning objectives

  • Describe Scala’s type system (variance, higher-kinded and path-dependent types) and its value-based approach to errors.
  • Model data as algebraic data types with sealed traits and case classes, and process it with exhaustive pattern matching.
  • Compose effectful computations with for-comprehensions, and explain what flatMap must satisfy for that to work.
  • Reason through the design of each exercise brief before writing code.

You should be able to write and run a small Scala 3 program and be comfortable with recursion and immutable data. The harder briefs (free monads, an effect system) assume you have used map and flatMap on Option or Future and want to know what is underneath.

Scala runs on the JVM, so memory is garbage collected. Case classes are immutable by default, and value classes let a wrapper type cost nothing at run time.

The type system is strong and static, with declaration-site variance (+A, -A), higher-kinded types (types that take type constructors, such as F[_]), and path-dependent types (types that belong to a particular value). It is expressive enough to encode a state machine in types, which is what the phantom-types brief asks you to do.

Errors are values: Option for absence, Either for a failure with a reason, Try for wrapping code that throws, and recovery combinators on Future. There are no checked exceptions. Concurrency comes from Future and Promise in the standard library and from ecosystems built on top: Akka actors, cats-effect IO, and ZIO. Builds use sbt, scala-cli, or Mill. The exercises target Scala 3.

  • Case classes for immutable data.
  • Pattern matching with exhaustiveness checking, so the compiler warns about a missed case.
  • For-comprehensions for monadic composition.
  • Given instances (Scala 3’s replacement for implicits) for type-class derivation.
  • Sealed traits for algebraic data types.
  • Extension methods (Scala 3) instead of implicit classes.
  • Opaque types for zero-cost newtype wrappers.

The briefs are listed below, ordered by concept rather than by difficulty, so choose by the difficulty rating (1 is easiest, 5 hardest) rather than by number.

#ExerciseConceptsDifficulty (1-5)
01Immutable red-black treeCase classes, pattern matching, recursion3
02Monad implementationHigher-kinded types, flatMap, for-comprehension4
03Actor system (simplified)Message passing, mailboxes, supervision4
04Type-safe builder (phantom types)Phantom types, type-level state machine4
05Streaming CSV parserIterator, lazy evaluation, fs2-style3
06JSON codec (automatic derivation)Given instances, Mirror, compile-time derivation5
07Parallel collection processorFuture, ExecutionContext, Par3
08Free monad interpreterGADTs, natural transformations, Church encoding5
09Property-based test frameworkGenerators, shrinking, given Arbitrary4
10Effect system (mini-ZIO)Trampolining, fibers, error channels5
  • Case class: an immutable Scala class whose constructor parameters become fields and for which the compiler generates structural equality, copying, and pattern-matching support; Scala’s standard way to model data.
  • Sealed trait: a Scala trait whose subtypes must all be defined in the same file, so the compiler knows every case and checks pattern matches for exhaustiveness; with case classes it encodes algebraic data types.
  • Higher-kinded type: a type parameterized by a type constructor rather than a type, such as F[_], which lets one piece of code abstract over containers like List, Option, and Future.
  • Path-dependent type: a type that belongs to a particular value, written x.T, so the member types of two different instances are distinct and cannot be mixed.
  • For-comprehension: Scala syntax that desugars a sequence of generators into nested flatMap calls and a final map, giving sequential-looking code for Option, Future, and other monads.
  • Given instance: a Scala 3 value declared with given that the compiler supplies automatically wherever a parameter of its type is required; it replaces implicits for type-class instances and derivation.
  • Opaque type: a Scala 3 type alias whose underlying representation is visible only where it is defined, giving a distinct type with no run-time cost; a newtype without a wrapper.