How do I declare the return variable inside a switch expression in EK9?

← Control Flow · Ref: Q938

A switch expression requires a return variable declaration immediately after the switch line. Each case assigns to that variable.

PATTERN

  result := switch value
    <- label as String: String()
    case "A"
      label: "Alpha"
    case "B"
      label: "Beta"
    default
      label: "Other"

The '<- label as String: String()' declares a variable named 'label' initialized to an empty String. Each case assigns to label. After the switch, result holds label's final value.

DECLARATION FORM

  result <- switch expr
    <- rtn as Type?                  // Unset, nullable style
  result := switch expr
    <- rtn as Type: initialValue     // With default
  result := switch expr
    <- rtn <- Type()                 // Constructor initialization

THE VARIABLE NAME IS YOURS TO CHOOSE

Use any meaningful name: rtn, category, label, outcome. The name should describe what the switch computes.

NO RETURN STATEMENT

EK9 has no return keyword. The declared variable's final value IS the result. The compiler verifies every path assigns it.

See Q68 for switch expression basics. See Q901 for expression vs statement. See Q782 for return rules.

Example

defines module qa.controlflow.switchreturn

  defines function

    classifyHttpCode() as pure
      -> code as Integer
      <-
        rtn as String: String()

      rtn := switch code
        <- category as String: String()
        case 200
          category: "OK"
        case 301
          category: "Redirect"
        case 404
          category: "Not Found"
        case 500
          category: "Server Error"
        default
          category: "Unknown"

    describePriority() as pure
      -> level as Integer
      <-
        rtn as String: String()

      rtn := switch level
        <- desc as String: String()
        case 1
          desc: "Critical"
        case 2
          desc: "High"
        case 3
          desc: "Medium"
        default
          desc: "Low"

  defines program

    SwitchReturnDemo()
      stdout <- Stdout()

      httpResult <- classifyHttpCode(404)
      stdout.println(`HTTP 404: ${httpResult}`)

      okResult <- classifyHttpCode(200)
      stdout.println(`HTTP 200: ${okResult}`)

      priority <- describePriority(1)
      stdout.println(`Priority 1: ${priority}`)

      lowPriority <- describePriority(9)
      stdout.println(`Priority 9: ${lowPriority}`)

Common mistakes

E07405 — Switch expressions require a return variable declaration (<- rtn as Type). Without it, the switch cannot produce a value. See ek9 -h E07405 for details.

Incorrect:

      rtn := switch code

Correct:

      rtn := switch code
        <- category as String: String()
Other ways to ask this
  • What is the <- rtn pattern in switch expressions?
  • How does switch return a value in EK9?
  • Why do I need a return declaration inside switch?

Coming from another language?

Java: switch expression with yield keyword. Kotlin: when returns last expression in branch. Rust: match returns last expression. C#: switch expression with =>. EK9: explicit return variable declaration with <- inside the switch block.

Keywords: switch, case, return, assign, result, variable, declaration, expression