Can switch return a value (switch as expression)?
← Control Flow · Ref: Q68
Yes. EK9's switch can return a value by using the expression form. You declare a return variable inside the switch, each case assigns to it, and the whole switch becomes an expression that can be assigned to a variable.
BASIC SWITCH EXPRESSION
Assign the result of a switch:
dayName <- switch dayNumber <- rtn as String? case 1 rtn: "Monday" case 2 rtn: "Tuesday" default rtn: "unknown"
The outer '<- dayName' captures the switch result. The inner '<- rtn as String?' declares the return variable. Each case assigns to 'rtn', and its final value is returned.
RETURN VARIABLE DECLARATION
The return variable appears right after the switch line, indented:
result <- switch controlValue <- rtn as String? case ...
The '<- rtn as String?' declares a variable named 'rtn' of type String, initially unset. You can also initialise it: '<- rtn <- String()' or '<- rtn as String: "default"'.
NO RETURN STATEMENT
EK9 has no 'return' keyword. The value is returned implicitly through the declared return variable. Every path (all cases plus default) should set the variable.
BOOLEAN SWITCH AS TERNARY
A Boolean switch expression works as a ternary alternative:
label <- switch isEnabled <- rtn as String? case true rtn: "ON" default rtn: "OFF"
This replaces the ternary operator from other languages.
EK9 also provides a dedicated ternary syntax for simple cases:
label <- isEnabled <- "ON" else "OFF"
STATEMENT VS EXPRESSION
The statement form assigns to pre-declared variables (see Q63). The expression form creates a new variable from the result. Use expression form when the switch's purpose is to compute a single value.
Switch expressions work with all the same features: multiple case values (see Q69), comparison operators (see Q70), pattern matching (see Q71), given/when keywords (see Q72), and exhaustive enum matching (see Q73).
See Q63 for basic switch statement form. See Q61 for if/else as an alternative.
Example
defines module qa.flow.switch.expression defines function supplyDay() as pure -> dayInput as Integer <- rtn as Integer: dayInput supplyEnabled() as pure -> enabled as Boolean <- rtn as Boolean: enabled supplyCommand() as pure -> cmd as String <- rtn as String: cmd supplyPriority() as pure -> level as Integer <- rtn as Integer: level defines program SwitchExpressionDemo() stdout <- Stdout() // === BASIC SWITCH EXPRESSION === dayNumber <- supplyDay(3) dayName <- switch dayNumber <- rtn as String? case 1 rtn: "Monday" case 2 rtn: "Tuesday" case 3 rtn: "Wednesday" case 4 rtn: "Thursday" case 5 rtn: "Friday" default rtn: "weekend or invalid" stdout.println(`Day ${dayNumber}: ${dayName}`) // === BOOLEAN SWITCH AS TERNARY ALTERNATIVE === isEnabled <- supplyEnabled(true) statusLabel <- switch isEnabled <- rtn as String? case true rtn: "ON" default rtn: "OFF" stdout.println(`Status: ${statusLabel}`) // === STRING SWITCH EXPRESSION === command <- supplyCommand("start") response <- switch command <- rtn as String? case "start" rtn: "Starting service..." case "stop" rtn: "Stopping service..." case "restart" rtn: "Restarting service..." default rtn: "Unknown command" stdout.println(response) // === ASSIGNING EXPRESSION RESULT === priority <- supplyPriority(2) urgencyLabel <- switch priority <- rtn as String? case 1 rtn: "CRITICAL" case 2 rtn: "HIGH" case 3 rtn: "MEDIUM" default rtn: "LOW" stdout.println(`Priority ${priority}: ${urgencyLabel}`)
Common mistakes
E01072 — EK9 has no return statement. Switch expressions use a declared return variable (rtn) that is assigned in each case. The final value is returned implicitly. See ek9 -h E01072 for details.
Incorrect:
switch dayNumber case 1 return "Monday"
Correct:
dayName <- switch dayNumber <- rtn as String? case 1 rtn: "Monday"
E01070 — EK9 has no break statement. Each switch case is self-contained with no fallthrough, making break unnecessary and nonexistent in the grammar. See ek9 -h E01070 for details.
Incorrect:
case 1 rtn: "Monday" break case 2 rtn: "Tuesday" break
Correct:
case 1 rtn: "Monday" case 2 rtn: "Tuesday"
Other ways to ask this
- How do I use switch as an expression in EK9?
- How do I assign a value from a switch statement?
- How does switch expression work in EK9?
Coming from another language?
Java: switch expressions since Java 14 with arrow syntax and yield keyword. Rust: match is always an expression (returns a value). Kotlin: when is always an expression when exhaustive. Python: no switch expression (use dict mapping or if/else). Go: no switch expression form. C/C++: no switch expression (ternary for simple cases). C#: switch expressions since C# 8 with arrow syntax. JavaScript: no switch expression. Swift: no switch expression but if/switch can be used in return. EK9: switch expression with declared return variable, no return keyword, value returned implicitly through the variable, works with all switch features.
Keywords: guard, error, flow, assign, return, value, switch, compute, expression, condition, ok, given, control, result, ternary, branch