GALA-E0141 — Unreachable case

When it fires. A match or a partial-function literal { case ... } has a case written after an unguarded case that matches every value. Cases are tried in the order they are written, so the later case can never run. These cases match every value:

A typed pattern counts when it spells the subject’s type as the subject’s type is written. In a partial-function literal, a typed or tuple pattern counts only when the expected function type gives the parameter’s type. Other patterns are not counted, even one that happens to match every value, such as an extractor whose fields are all bindings.

A case with a guard, case x if x > 0, does not match every value, so the cases after it stay reachable. In a match, the second of two default cases in a row is GALA-E0006 instead; that page says exactly which cases are defaults. In a partial-function literal it is this error too.


Code that triggers it

package main

func name(n int) string = n match {
    case 0 => "zero"
    case _ => "other"
    case 1 => "one"
}

func main() {
    Println(name(1))
}

Compiler message

error[GALA-E0141]: unreachable case: `case _` on line 5 matches every value, so no case after it can run
  --> main.gala:6:5
  |
6 |     case 1 => "one"
  |     ^^^^ move this case above `case _`, or delete it
  |
  = hint: move this case above `case _`, or delete it

How to fix it

Move the catch-all to the end, so the specific cases are tried first:

func name(n int) string = n match {
    case 0 => "zero"
    case 1 => "one"
    case _ => "other"
}

If the later case really is dead, delete it.


Why the rule exists

GALA follows Scala’s first-match semantics: the first case that matches wins. A match used to move its catch-all to the end, so the cases written after it ran first and won, and name(1) above returned "one" without a diagnostic. A partial-function literal kept the written order, so the same cases gave different answers in the two forms. Rejecting the case makes the order of the cases the order they run in, in both forms.