0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Enums in Scala 3

New enum syntax.

Enums in Scala 3 is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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"))

Ordinal and Name

Each enum value has an ordinal (zero-based position) and a generated toString using its case name.

enum Color:
  case Red, Green, Blue

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

Enums with Parameters

Enum cases can take constructor parameters, giving each value associated data. This is useful for storing constants like RGB codes or planet masses.

enum Planet(val mass: Double):
  case Earth extends Planet(5.97e24)
  case Mars  extends Planet(6.42e23)

object Main:
  def main(args: Array[String]): Unit =
    println(Planet.Earth.mass)
    println(Planet.Mars.mass)

Methods on Enums

You can define methods inside an enum body. They are available on every value, like methods on a class.

enum Direction:
  case North, South, East, West
  def opposite: Direction = this match
    case North => South
    case South => North
    case East  => West
    case West  => East

object Main:
  def main(args: Array[String]): Unit =
    println(Direction.North.opposite)

Generic ADT Enums

Enums can be generic and parameterized, letting you model algebraic data types. Here is a simplified Option type.

enum Maybe[+A]:
  case Just(value: A)
  case Nothing

object Main:
  def main(args: Array[String]): Unit =
    val a: Maybe[Int] = Maybe.Just(42)
    println(a)

Recursive ADT Enums

Enums shine for recursive structures. A binary tree or linked list reads cleanly as a set of cases.

enum Tree[+A]:
  case Leaf(value: A)
  case Branch(left: Tree[A], right: Tree[A])

object Main:
  def main(args: Array[String]): Unit =
    val t = Tree.Branch(Tree.Leaf(1), Tree.Leaf(2))
    println(t)

Pattern Matching on Enums

Because enums are sealed, the compiler can check exhaustiveness of a match. Missing a case triggers a warning.

enum Light:
  case Red, Yellow, Green

object Main:
  def action(l: Light): String = l match
    case Light.Red    => "stop"
    case Light.Yellow => "slow"
    case Light.Green  => "go"

  def main(args: Array[String]): Unit =
    println(action(Light.Green))

Java-Compatible Enums

Add extends java.lang.Enum behavior by deriving from scala.reflect.Enum automatically; for true Java interop, write enum X extends Enum[X]... but most code just uses the plain form which interops well with Java when needed.

enum Suit:
  case Hearts, Diamonds, Clubs, Spades

object Main:
  def main(args: Array[String]): Unit =
    println(Suit.values.length)
    println(Suit.Spades.ordinal)

Combining Fields and Methods

You can mix parameterized cases with shared methods for rich domain models.

enum Currency(val symbol: String):
  case USD extends Currency("$")
  case EUR extends Currency("\u20ac")
  def format(amount: Double): String = s"$symbol$amount"

object Main:
  def main(args: Array[String]): Unit =
    println(Currency.USD.format(9.99))

When to Use enum

Reach for enum when you have a closed set of choices or an algebraic data type.

  • Simple cases for plain enumerations.
  • Parameterized cases for ADTs and sum types.
  • Free exhaustiveness checks make refactors safe.
enum Result[+E, +A]:
  case Ok(value: A)
  case Err(error: E)

object Main:
  def main(args: Array[String]): Unit =
    val r: Result[String, Int] = Result.Ok(7)
    println(r)

Quick Check

Test your understanding of Scala 3 enums.

Recap

You learned Scala 3 enums.

  • Concise enum replaces sealed-trait boilerplate.
  • Simple enums get values, valueOf, and ordinal.
  • Cases may take parameters for associated data.
  • Generic and recursive enums model ADTs.
  • Sealed nature enables exhaustive pattern matching.
enum Weekday:
  case Mon, Tue, Wed, Thu, Fri
  def isStart: Boolean = this == Mon

object Main:
  def main(args: Array[String]): Unit =
    println(Weekday.Mon.isStart)
    println(Weekday.values.length)

Frequently asked questions

Is the “Enums in Scala 3” lesson free?

Yes — the full text of “Enums in Scala 3” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Enums in Scala 3”?

New enum syntax. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enums in Scala 3” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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