Skip to main content

Statements

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

Mutability​

Variables are mutable by default:

Mux
Loading...

To make a variable immutable, use const:

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)
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
Mux
Loading...

Scope Rules​

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

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​

Mux
Loading...

Exhaustiveness Checking​

The compiler requires all cases to be covered:

Mux
Loading...

Guard Conditions​

Arms can have additional if conditions:

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​

Mux
Loading...

Range Iteration​

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
Mux
Loading...

Condition Requirements​

The condition must evaluate to bool:

Mux
Loading...

Break Statements​

Semantics​

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

Continue Statements​

Semantics​

  1. Skips to the next iteration of the immediately enclosing loop
  2. The loop condition is checked again
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​

Mux
Loading...

Void Return​

Mux
Loading...

Early Returns​

Returns can appear anywhere in a function:

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​