0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Algebraic Data Types

Model your domain.

What Is an ADT?

An algebraic data type (ADT) is a type built by combining other types. In Scala you model ADTs with a sealed trait plus case class and case object variants.

ADTs let you precisely describe the shape of your domain data.

Sum Types

A sum type means a value is one of several alternatives (an OR). A traffic light is Red OR Yellow OR Green.

You express this with a sealed trait and several variants.

sealed trait Light
case object Red extends Light
case object Yellow extends Light
case object Green extends Light
object Main {
  def main(args: Array[String]): Unit = {
    val l: Light = Yellow
    println(l)
  }
}

All lessons in this course

  1. Case Classes
  2. Sealed Traits
  3. Algebraic Data Types
  4. Exhaustive Matching
← Back to Scala for Backend Engineering & Functional Programming