E07320: 'default' is required in this 'switch' statement

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

What went wrong

Your switch STATEMENT (not expression) is missing a default case. ALL switch statements require a default case.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 147 (Why doesn't EK9 have switch fallthrough and how do I grou...)
  • ek9 -q 69 (How do I match multiple values in one case?)

Rationale

Every switch must have a default to handle unexpected or unset values. For enum switches, even when all values are covered, the variable could be unset (EK9 tri-state semantics), so default handles that state.

How to fix it

  1. Add a default case

Common causes

  • Forgot to add default case
  • Thinking all enum cases are covered (but unset state still possible)

See also

  • E07320: non-enum switch missing default (this error)
  • E07310: enum switch missing cases
  • 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 E07320: 'default' is required in this 'switch' statement

#!ek9
defines module error.e07320.bad

  defines function

    allCasesThrowNoDefault()
      -> val as String
      <- rtn as String: "start"

      switch val
        case "This"
          throw Exception("This")
        case "That"
          throw Exception("That")

      rtn += "!"

Fixed: Add a default

#!ek9
defines module error.e07320.good

  defines function

    describe()
      -> word as String
      <- rtn as String: "start"

      //A default handles every other value, and an unset one
      switch word
        case "This"
          rtn: "this"
        case "That"
          rtn: "that"
        default
          rtn: "something else"

Questions and answers

These Q&A entries show E07320 and its fix: