E07310: 'cases' should cover all enumerated values in 'switch'

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: NOT_ALL_ENUMERATED_VALUES_PRESENT_IN_SWITCH · All error codes

What went wrong

Your switch statement on an ENUMERATION doesn't handle all possible enum values. When switching on an enumeration with simple value cases, ALL enum values must be covered AND a default case is required.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 792 (Why must a switch on an enumeration cover all values?)
  • ek9 -q 99 (How do I create an enumeration in EK9?)
  • ek9 -q 100 (How do EK9 enumerations differ from Java enums?)

Rationale

EXHAUSTIVE switches on enums catch bugs when new values are added. The default case handles the UNSET state (EK9 tri-state semantics), since an enum variable can exist but not yet hold a meaningful value.

How to fix it

  1. Add all missing enum cases
  2. Add a default case for the unset state

Common causes

  • Forgot some enum values
  • Enum was extended but switch not updated

See also

  • E07310: enum switch missing cases (this error)
  • E07320: non-enum switch missing default
  • E07330: switch expression missing default

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07310: 'cases' should cover all enumerated values in 'switch'

#!ek9
defines module error.e07310.bad

  defines type

    Signal
      Red
      Amber
      Green

  defines function

    meaning()
      -> signal as Signal
      <- rtn as String: String()

      //Amber and Green are not handled
      description <- switch signal
        <- text as String: "unknown"
        case Signal.Red
          text: "stop"
        default
          text: "not set"

      rtn: description

Fixed: Handle every value, and the unset one

#!ek9
defines module error.e07310.good

  defines type

    Signal
      Red
      Amber
      Green

  defines function

    meaning()
      -> signal as Signal
      <- rtn as String: String()

      //Every value is handled, and the default handles an unset signal
      description <- switch signal
        <- text as String: "unknown"
        case Signal.Red
          text: "stop"
        case Signal.Amber
          text: "wait"
        case Signal.Green
          text: "go"
        default
          text: "not set"

      rtn: description

Questions and answers

These Q&A entries show E07310 and its fix: