Contents

Haze is imperative enough to accumulate a total in a loop and functional enough to build a new list without touching the old one. The distinction between rebinding a variable and mutating an object is the key to using both styles safely.

Const and Var#

A const binding cannot be reassigned. A var binding can be reassigned, and its fields or elements can be mutated when the type allows it. Both forms require an initializer; there are no uninitialized locals.

const name = "Ada"
var count = 0
count = count + 1

const fixed = [1, 2, 3]
// fixed = [4, 5, 6]       // error: cannot assign to 'fixed'

A type annotation may appear after the name, separated by a colon. The annotation is required for an empty list literal because an empty list has no element type.

var values List[i32] = []
var total i64 = 0
const label str = "total"

The parser also accepts a declaration without a colon when the type is followed directly by =, as in var total i64 = 0. The formatter writes the colon form for variables and the space form for typed globals; consistency matters more than which spelling is used.

Bindings Are Not Values#

A binding is a name for a location. Assigning to the name replaces the value in that location. Mutating a field or an index changes the object the binding refers to. Those are different operations, and the difference is visible when two bindings refer to the same managed object.

type Counter {
    n i32
}

var first = Counter { n = 1 }
var second = first
second.n = 9
print(first.n)       // 9: both bindings refer to the same record

The same rule applies to lists. Reassigning a list variable to a new list does not change other bindings, but assigning an element does.

var left = [1, 2]
var right = left
right[0] = 9
print(left[0])       // 9

right = [7, 8]
print(left[0])       // 9: reassignment changed only 'right'

Scalars are copied on assignment. A managed value is represented by a reference, so assignment copies the reference, not the object. The table below summarizes the practical behaviour.

Value category Assignment copies Field or index mutation visible through aliases Rebinding visible through aliases
Integer, float, bool, rune, enum The value Not applicable No
str The string reference Strings are immutable No
Record, sum, Option, Result The reference Yes, unless the type's invariants forbid the individual write No
List[T], bytes The reference Yes No
Closure The reference Captured state is by value, object state may be shared No
ptr, cstr, extern record The raw address or C-layout value Depends on the pointed-to memory No

Mutation and Invariants#

A record type with a cross-field invariant cannot have one field assigned in isolation, because the write could break the relationship between fields. Replace the whole value instead.

type Positive = i32 : invariant self > 0

type Window : invariant lo <= hi {
    lo Positive
    hi Positive
}

fn widen(w Window, by i32) Result[Window, str] {
    const lo = Positive { w.lo - by }?
    const hi = Positive { w.hi + by }?
    return Success(Window { lo = lo, hi = hi }?)
}

A record without a cross-field invariant may allow assignment to a single refinement-typed field, but the assigned expression must itself establish the invariant. A const binding forbids assignment through it, including field and index writes.

Parameters and Argument Passing#

Function parameters are local bindings. Scalars are passed by value. Managed values are passed by reference, so a function that mutates a field of a record parameter changes the caller's object even though the parameter itself is a local binding.

type Counter {
    n i32
}

fn bump(c Counter) {
    c.n += 1
}

fn main() {
    var c = Counter { n = 1 }
    bump(c)
    print(c.n)       // 2
}

This behaviour is useful for small internal helpers and dangerous for public APIs. The idiomatic chapter recommends returning a new value rather than mutating a parameter unless mutation is the documented purpose of the function.

Reassignment and Compound Assignment#

Plain assignment replaces the value in a binding. Compound assignment is available for integers and for list append. The operators are statements; they do not produce a value.

var n = 10
n += 5
n -= 3
n *= 2
n /= 4
n %= 5

var xs = [1, 2]
xs += [3]

List append with += also accepts another list. The result is a list with the elements of both operands. A single-element right-hand side is optimized by the compiler, but the meaning is the same as concatenation.

var xs = [1, 2]
xs += [3, 4]
xs += xs

++ and -- are postfix statements equivalent to += 1 and -= 1. They are not expressions, so they cannot be used inside a larger expression.

var i = 0
i++
i--

Compound assignment on a refinement type is rejected because a raw arithmetic result does not automatically satisfy the invariant. Build the refined value explicitly with checked construction.

Scope and Shadowing#

A block introduces a scope. A declaration is visible from its declaration to the end of the enclosing block. Inner scopes may shadow outer names, and a later declaration in the same scope may also shadow an earlier one. Shadowing is occasionally useful for a refined value that replaces a raw one, but it should not be used to recycle a name with a different meaning.

fn main() {
    const n = 10
    {
        const n = 20
        print(n)     // 20
    }
    print(n)         // 10
}

Loop variables are scoped to the loop body. A defer captures the lexical binding that was visible when the defer was registered, so a later shadowing declaration does not change what the deferred code refers to. See Defer for the full rule.

Module Constants#

A top-level const declares a module constant. Module constants are immutable, may be declared before or after their use, and may be used in contracts and type invariants. There are no mutable globals.

const LIMIT i32 = 100

const DOUBLE_LIMIT i32 = LIMIT * 2

fn below(n i32) bool => n < LIMIT

Module constants are evaluated at compile time when their initializers can be folded. A constant that depends on an impure call is not a compile-time constant and cannot be used where the compiler requires a constant expression.