E07330: 'default' is required in this 'switch' expression

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

What went wrong

Your switch EXPRESSION (used in assignment) is missing a default case. Switch expressions must return a value for ALL possible inputs.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 788 (Why does a switch expression require a default case in EK9?)

Rationale

Switch EXPRESSIONS must be exhaustive to guarantee a return value.

How to fix it

  1. Add default case that returns a value

Common causes

  • Forgot default case
  • Not realizing expression requires exhaustive coverage

See also

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

Examples

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

Fails with E07330: 'default' is required in this 'switch' expression

#!ek9
defines module error.e07330.bad

  defines function

    sizeOf()
      -> count as Integer
      <- rtn as String: String()

      //Without a default, size has no value when count is 10 or more, or unset
      size <- switch count
        <- text as String?
        case < 10
          text: "small"

      rtn: size

Fixed: Add a default

#!ek9
defines module error.e07330.good

  defines function

    sizeOf()
      -> count as Integer
      <- rtn as String: String()

      size <- switch count
        <- text as String: String()
        case < 10
          text: "small"
        default
          text: "large"

      rtn: size

Questions and answers

These Q&A entries show E07330 and its fix: