E029: Pattern Match Exhaustivity
This warning is emitted when a pattern match expression may not handle all possible input values. The compiler detects cases that are not covered by any of the match cases.
There are several ways to make the match exhaustive:
- Add missing cases as shown in the warning
- If an extractor always returns
Some(...), writeSome[X]for its return type - Add a
case _ => ...at the end to match all remaining cases
Example
enum Color:
case Red, Green, Blue
def describe(c: Color): String = c match
case Color.Red => "red"
case Color.Green => "green"
Warning
-- [E029] Pattern Match Exhaustivity Warning: example.scala:4:33 ---------------
4 |def describe(c: Color): String = c match
| ^
| match may not be exhaustive.
|
| It would fail on pattern case: Blue
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| There are several ways to make the match exhaustive:
| - Add missing cases as shown in the warning
| - If an extractor always return Some(...), write Some[X] for its return type
| - Add a case _ => ... at the end to match all remaining cases
-----------------------------------------------------------------------------
Solution
// Add the missing case
enum Color:
case Red, Green, Blue
def describe(c: Color): String = c match
case Color.Red => "red"
case Color.Green => "green"
case Color.Blue => "blue"
// Alternative: add a wildcard case for remaining patterns
enum Color:
case Red, Green, Blue
def describe(c: Color): String = c match
case Color.Red => "red"
case Color.Green => "green"
case _ => "other"
In this article