Skip to main content

Statements

This document describes the semantics of each statement type in Mux.

Mutability

Variables are mutable by default:

snippet.mux
Loading...

To make a variable immutable, use const:

snippet.mux
Loading...

Constant Declaration Statements

Rules

  1. Constants must be initialized at declaration
  2. Constants cannot be reassigned
  3. Constants cannot be used with increment/decrement operators
  4. Constants can be declared at:
    • Top-level (module scope)
    • Inside classes (class fields)
    • Inside functions (local constants)
snippet.mux
Loading...

If Statements

Semantics

  1. The condition expression must evaluate to bool
  2. Only the matched branch is executed
  3. Each branch creates a new scope
snippet.mux
Loading...

Scope Rules

Variables declared in one branch are not accessible in other branches:

snippet.mux
Loading...

Match Statements

Semantics

  1. The match expression is evaluated
  2. Each arm is checked in order
  3. The first matching arm (with optional guard) is executed
  4. Exhaustiveness is checked at compile time

Patterns

PatternMeaningExample
_Wildcard (matches anything)_ { print("other") }
Variant(value)Destructuring enumsome(v) { print(v) }
IdentifierBind to variablePoint(x, y) { ... }
valueLiteral match0 { print("zero") }

Examples

snippet.mux
Loading...

Exhaustiveness Checking

The compiler requires all cases to be covered:

snippet.mux
Loading...

Guard Conditions

Arms can have additional if conditions:

snippet.mux
Loading...

For Statements

Semantics

  1. The iterable expression is evaluated
  2. Each element is bound to the pattern
  3. The body is executed for each element

Iteration Patterns

snippet.mux
Loading...

Range Iteration

snippet.mux
Loading...

While Statements

Semantics

  1. The condition is evaluated
  2. If true, the body is executed and the condition is checked again
  3. If false, execution continues after the loop
snippet.mux
Loading...

Condition Requirements

The condition must evaluate to bool:

snippet.mux
Loading...

Break Statements

Semantics

  1. Exits the immediately enclosing loop
  2. Execution continues after the loop
snippet.mux
Loading...

Continue Statements

Semantics

  1. Skips to the next iteration of the immediately enclosing loop
  2. The loop condition is checked again
snippet.mux
Loading...

Return Statements

Semantics

  1. Returns control from the current function
  2. If a value is provided, it becomes the function's result
  3. If no value is provided, the function must have return type void

Value Return

snippet.mux
Loading...

Void Return

snippet.mux
Loading...

Early Returns

Returns can appear anywhere in a function:

snippet.mux
Loading...

Cleanup on Return

When a return executes:

  1. All variables in current scope are cleaned up (RC decremented)
  2. Nested scopes are cleaned up in reverse order
  3. Control transfers to the caller

See Also