Skip to content

40 Exhaustive matching ​

A switch over a closed enum must account for every variant.

norm
enum State {
  Ready,
  Running,
  Done
}

String label(State state) {
  return switch state {
    case Ready { break "ready" }
    case Running { break "running" }
    case Done { break "done" }
  }
}

main() {
  printLine(label(state: State.Running))
}

Expected output:

text
running

All three cases are present, so the result has a path for every State. If a new variant is added, update the switch too; the compiler reports an uncovered case instead of silently choosing one.

This separate example deliberately omits Done and does not run:

norm
enum State {
  Ready,
  Running,
  Done
}

String label(State state) {
  return switch state {
    case Ready { break "ready" }
    case Running { break "running" }
  }
}

norm check reports NORM-FLOW-0001: switch is not exhaustive. The compiler test checks that diagnostic against this file.

Try it: Add the missing Done case to the rejected example and check it again.

Precise rules: Reference.

Norm 0.25