Can switch be used as an expression in EK9?

← Control Flow · Ref: Q901

YES — switch CAN be an expression in EK9. Like Kotlin's 'when' expression, EK9's switch can produce a value that gets assigned.

SWITCH AS EXPRESSION

Use <- to capture the switch result. The switch needs a return declaration:

  label <- switch category
    <- rtn as String: String()
    case "A"
      rtn: "Alpha"
    default
      rtn: "Unknown"

The '<- rtn as String' declares the return variable. Each case assigns to rtn.

SWITCH AS STATEMENT

Without <-, switch is a statement:

  switch direction
    case "north"
      moveNorth()
    default
      stay()

NO FALLTHROUGH

EK9 has NO switch fallthrough — each case is independent. No break needed because break does not exist. Multiple values on one case:

  switch dayOfWeek
    case "MONDAY", "TUESDAY", "WEDNESDAY"
      rtn: "weekday"
    case "SATURDAY", "SUNDAY"
      rtn: "weekend"

See Q63 for switch basics. See Q68 for switch as expression. See Q782 for switch return rules.

Example

defines module qa.controlflow.switchexpr

  defines function

    <?-
      Switch as expression — like Kotlin's when.
      Needs <- rtn declaration inside switch.
    -?>
    classifyTemperature() as pure
      -> degrees as Float
      <-
        rtn as String: String()

      rtn := switch degrees
        <- category as String: String()
        case < 0.0
          category: "freezing"
        case < 10.0
          category: "cold"
        case < 20.0
          category: "cool"
        case < 30.0
          category: "warm"
        default
          category: "hot"

    <?-
      Multiple case values — no fallthrough needed.
    -?>
    isWeekend() as pure
      -> dayName as String
      <-
        rtn as Boolean: false

      rtn := switch dayName
        <- weekend as Boolean: false
        case "SATURDAY", "SUNDAY"
          weekend: true
        default
          weekend: false

    <?-
      Switch as expression returning String.
    -?>
    describeSeason() as pure
      -> month as Integer
      <-
        rtn as String: String()

      rtn := switch month
        <- seasonName as String: String()
        case 3, 4, 5
          seasonName: "Spring"
        case 6, 7, 8
          seasonName: "Summer"
        case 9, 10, 11
          seasonName: "Autumn"
        default
          seasonName: "Winter"

  defines program

    SwitchExpressionDemo()
      stdout <- Stdout()

      forecast <- classifyTemperature(15.5)
      stdout.println(`15.5 degrees is: ${forecast}`)

      hot <- classifyTemperature(35.0)
      stdout.println(`35.0 degrees is: ${hot}`)

      saturdayWeekend <- isWeekend("SATURDAY")
      stdout.println(`Saturday weekend: ${saturdayWeekend}`)

      mondayWeekend <- isWeekend("MONDAY")
      stdout.println(`Monday weekend: ${mondayWeekend}`)

      season <- describeSeason(7)
      stdout.println(season)
Other ways to ask this
  • Is switch an expression or statement in EK9?
  • How do I assign from a switch in EK9?
  • Does EK9 switch return a value?
  • How does switch work as an expression in EK9?

Coming from another language?

Kotlin: when expression — EK9 switch as expression works similarly but needs explicit return declaration. Java: switch expressions (Java 14+) with -> — similar concept. Rust: match expression — similar, always returns a value. Go: switch is a statement, not an expression. Python: match (3.10+) is a statement. C: switch with mandatory break, fallthrough by default — EK9 has NO fallthrough and NO break. EK9: switch is BOTH expression and statement, like Kotlin's when. No fallthrough, no break needed.

Keywords: return, when, default, fallthrough, expression, case, switch, Kotlin, statement