Statements
This document describes the semantics of each statement type in Mux.
Mutability
Variables are mutable by default:
To make a variable immutable, use const:
Constant Declaration Statements
Rules
- Constants must be initialized at declaration
- Constants cannot be reassigned
- Constants cannot be used with increment/decrement operators
- Constants can be declared at:
- Top-level (module scope)
- Inside classes (class fields)
- Inside functions (local constants)
If Statements
Semantics
- The condition expression must evaluate to
bool - Only the matched branch is executed
- Each branch creates a new scope
Scope Rules
Variables declared in one branch are not accessible in other branches:
Match Statements
Semantics
- The match expression is evaluated
- Each arm is checked in order
- The first matching arm (with optional guard) is executed
- Exhaustiveness is checked at compile time
Patterns
| Pattern | Meaning | Example |
|---|---|---|
_ | Wildcard (matches anything) | _ { print("other") } |
Variant(value) | Destructuring enum | some(v) { print(v) } |
Identifier | Bind to variable | Point(x, y) { ... } |
value | Literal match | 0 { print("zero") } |
Examples
Exhaustiveness Checking
The compiler requires all cases to be covered:
Guard Conditions
Arms can have additional if conditions:
For Statements
Semantics
- The iterable expression is evaluated
- Each element is bound to the pattern
- The body is executed for each element
Iteration Patterns
Range Iteration
While Statements
Semantics
- The condition is evaluated
- If true, the body is executed and the condition is checked again
- If false, execution continues after the loop
Condition Requirements
The condition must evaluate to bool:
Break Statements
Semantics
- Exits the immediately enclosing loop
- Execution continues after the loop
Continue Statements
Semantics
- Skips to the next iteration of the immediately enclosing loop
- The loop condition is checked again
Return Statements
Semantics
- Returns control from the current function
- If a value is provided, it becomes the function's result
- If no value is provided, the function must have return type
void
Value Return
Void Return
Early Returns
Returns can appear anywhere in a function:
Cleanup on Return
When a return executes:
- All variables in current scope are cleaned up (RC decremented)
- Nested scopes are cleaned up in reverse order
- Control transfers to the caller
See Also
- Expressions - Expression evaluation
- Control Flow - Control flow guide