Contents

Haze keeps control flow explicit. Conditions are bool, loops have three distinct forms, and break and continue are statements scoped to the nearest loop. Pattern matching is the only way to inspect a sum value, which keeps variant handling visible.

Conditionals#

An if tests a boolean expression. A block body is enclosed in braces; a single indented statement is also accepted. An else may introduce another block or another if.

fn classify(n i32) str {
    if n < 0 {
        return "negative"
    } else if n == 0 {
        return "zero"
    } else {
        return "positive"
    }
}

When both branches produce a value, the if is an expression and its type is the common type of the branches. When there is no else, the expression is a statement and produces void.

const parity = if n % 2 == 0 { "even" } else { "odd" }

The condition must be bool. There is no implicit truthiness for integers, strings, lists, or pointers. This removes a large class of accidental conditions.

For-In Loops#

A for name in list loop visits each element of a list in order. The loop variable is scoped to the body. There is no index variable unless the loop is written over a range.

fn total(xs List[i32]) i32 {
    var sum = 0
    for x in xs {
        sum += x
    }
    return sum
}

The expression after in must be a list. Strings are not iterable as runes; use the rune functions described in Strings, Bytes, and Runes when character iteration is needed. The loop variable is a copy of the element for scalar element types and a reference for managed element types.

Range Loops#

A for name in lo..hi loop visits integers from lo inclusive to hi exclusive. Both bounds must be integers and are evaluated once, before the loop starts. The loop variable type follows the wider of the two bounds, with i32 as the default.

fn sum_to(n i32) i32 {
    var total = 0
    for i in 0..n {
        total += i
    }
    return total
}

A descending range is not expressed by reversing the bounds. Iterate upward and compute the value, or write a condition loop with an explicit decrement.

Condition Loops#

A for condition loop is the Haze spelling of a while loop. The condition is evaluated before each iteration. A bare for is an infinite loop and is equivalent to for true.

fn count_down(n i32) {
    var i = n
    for i > 0 {
        print(i)
        i -= 1
    }
}

fn forever() {
    for {
        if should_stop() {
            break
        }
    }
}

The condition must be bool. A bare for is primarily useful for a loop whose exit is expressed by break or by returning from the function.

Loop Invariants#

All three loop forms accept one or more : invariant clauses between the loop header and the body. The invariant is a boolean predicate over the loop variable and any values in scope. It is checked before the loop condition on every iteration, including the initial check before the first iteration.

fn save(account Account, amount i64, months i64) Account
    : pre months >= 0
{
    var acc = account
    for i in 0..months
        : invariant acc.balance == account.balance + amount * i
    {
        acc = deposit(acc, amount)
    }
    return acc
}

A constant invariant that is false is rejected during compilation. Invariants that the compiler cannot decide become runtime checks unless the program is built with -release. The Loop Invariants chapter explains the proof obligations and gives guidance on writing invariants that survive changes to the loop body.

Break and Continue#

break exits the nearest enclosing loop. continue skips the rest of the current iteration and proceeds to the loop's next condition check. Both are statements and carry no value.

for x in xs {
    if x < 0 {
        continue
    }
    if x > limit {
        break
    }
    print(x)
}

A break or continue outside a loop is rejected. A break or continue inside a defer body may only target a loop that is itself inside that defer body; using it to leave the deferred code is rejected. See Defer.

Matching#

match inspects a sum type, enum, Option, or Result. Each arm names a constructor and binds its fields. A wildcard arm uses _.

type Shape =
    | Circle(radius f64)
    | Rect(width f64, height f64)

fn area(shape Shape) f64 => match shape {
    Circle(radius) => 3.14159 * radius * radius
    Rect(width, height) => width * height
}

An enum pattern must be qualified with the enum type, as in Color.Red. A sum type pattern uses the variant name directly. Bindings are immutable in the arm. A _ binding discards a field without introducing a name.

type Color enum {
    Red
    Green
    Blue
}

fn code(colour Color) i32 => match colour {
    Color.Red => 1
    Color.Green => 2
    Color.Blue => 3
}

A match must be exhaustive. Write every variant when the type is closed and stable, and add a wildcard arm whenever the set of variants may grow. The compiler rejects a match that leaves a variant unhandled; the runtime fallback exists only as a defensive check for generated code.

If-Let Style Tests#

The is operator tests a value against a pattern and produces bool. In an if condition, the bindings introduced by the pattern are visible in the then branch. This is the concise way to handle a single variant without a full match.

if response is Success(value) {
    print(value)
} else {
    print("request failed")
}

if n is Positive(p) {
    print(p)
}

The else branch does not receive the bindings. If both outcomes need values, use match; if the test is about a refinement, is also checks the invariant at runtime.

Return and Implicit Results#

return exits the current function and optionally supplies a value. A function with a non-void return type must return a value on every path, or leave a final expression with a compatible type in a block body.

fn sign(n i32) i32 {
    if n < 0 {
        return -1
    } else if n > 0 {
        return 1
    }
    return 0
}

fn double(n i32) i32 {
    n * 2
}

The final-expression form is convenient for short functions. Use return when the control flow is not a straight line; mixing both forms in a long function tends to obscure the exit points.

Blocks and Scope#

Braces introduce a scope. Declarations inside the block are not visible after it closes. A block in expression position produces the value of its final expression.

const total = {
    const a = 10
    const b = 20
    a + b
}

defer is scoped to the block in which it is registered, and each loop iteration creates a fresh body scope. The Defer chapter describes the unwinding order in detail.