0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Enums in Scala 3

New enum syntax.

The New enum Keyword

Scala 3 adds a first-class enum construct. It replaces the verbose sealed-trait-plus-case-objects pattern from Scala 2 with concise syntax.

  • Each case becomes a value of the enum type.
  • The compiler generates useful helpers automatically.
enum Color:
  case Red, Green, Blue

object Main:
  def main(args: Array[String]): Unit =
    println(Color.Red)

Listing All Values

Every simple enum gets a companion with values (an array of all cases) and valueOf (lookup by name).

enum Color:
  case Red, Green, Blue

object Main:
  def main(args: Array[String]): Unit =
    Color.values.foreach(println)
    println(Color.valueOf("Green"))

All lessons in this course

  1. Significant Indentation
  2. Enums in Scala 3
  3. Opaque Types
  4. Union and Intersection Types
← Back to Scala for Backend Engineering & Functional Programming