Contents

Haze expressions are built from literals, names, calls, field access, indexing, unary and binary operators, and a small set of control-flow expressions. Assignment is a statement, not an expression, which keeps the evaluation order of ordinary expressions predictable.

Precedence#

Operators are listed from lowest to highest precedence. All binary operators are left-associative except the ones whose syntax requires a right-hand form. Parentheses may always be used to make the intended grouping explicit.

Level Operators Notes
1 \|\| Short-circuit logical or
2 && Short-circuit logical and
3 \| Bitwise or
4 ^ Bitwise exclusive or
5 & Bitwise and
6 ==, != Equality
7 is Pattern test
8 <, <=, >, >= Ordering
9 <<, >> Shifts
10 +, - Addition, subtraction, string and list concatenation
11 *, /, % Multiplication, division, remainder
12 -, !, ~ Unary minus, logical not, bitwise complement
13 call, field, index, ? Postfix forms

The is operator sits between equality and comparison because its right-hand side is a pattern, not an ordinary expression. A ? applies to the expression immediately to its left.

Arithmetic#

Integer arithmetic unifies the operand types. If both operands have the same signedness, the result uses the wider rank, with a minimum of i32 or u32. If the signedness differs, the result follows the rank rules implemented by the compiler; in practice, mixing signed and unsigned values is a source of surprise and should be avoided in application code.

Expression Result type Note
i32 + i32 i32 No automatic promotion to i64
i8 + i8 i32 Narrow integers are promoted for arithmetic
i32 + i64 i64 Wider signed operand wins
u32 + u32 u32 Unsigned arithmetic
i32 + u32 u32 or i64 depending on rank Prefer explicit conversion
f64 + f64 f64 The only floating-point type
i32 + f64 f64 Numeric coercion applies

Narrowing is never implicit. Assigning an i64 to an i32 requires an explicit conversion, and a constant is checked for range at compile time.

var wide i64 = 10
var narrow i32 = trunc[i32](wide)
var widened i64 = cast[i64](narrow)

% is defined for integers only; applying it to floating-point values is an error. Unary minus requires a signed integer or f64, and unary complement requires an integer. ! requires bool.

Runtime integer arithmetic uses the width and signedness of the operand type. Compile-time folding rejects a constant result that does not fit the target type, so a contract or constant expression cannot silently wrap. At runtime, use a wider type or an explicit check when overflow is possible.

Comparison and Equality#

Ordering comparisons require two numeric operands of compatible type, or two runes. They produce bool. Equality is more restrictive: Haze compares integers, floats, booleans, strings, runes, pointers, C strings, and enum values of the same type. Records, lists, closures, and sum values are not comparable with ==; compare a meaningful field or write a predicate instead.

const same = "haze" == "haze"
const ordered = 3 < 8
const colour = Color.Red == Color.Red

There is no structural equality operator for records or lists. A type that needs equality should expose a function or implement a trait such as Eq.

Logical Operators#

&& and || require bool operands and short-circuit. The right-hand side is not evaluated when the left-hand side determines the result. This makes guarded expressions safe.

fn safe_ratio(n i32, d i32) i32
    : pre d != 0
=> if d == 0 { 0 } else { n / d }

The if expression is preferred for clarity when both branches produce a value. Short-circuiting is most useful inside contracts, where it prevents a division by zero in the unselected branch.

Bitwise and Shift Operators#

Bitwise operators &, |, ^, and ~ require integer operands. Shifts << and >> require an integer value and an integer shift count. Narrow integer operands are promoted to i32 or u32 for the operation, and a right shift of an unsigned value is logical.

const mask = 0xFF
const low = (value & mask)
const doubled = value << 1
const high = cast[u32](value) >> 24

Bitwise operators are not defined for f64. Shift counts outside the width of the value are not statically rejected by the type checker; keep shift counts in range as part of the function's precondition when the input is untrusted.

Conversion#

Two conversion forms exist. cast[T](value) converts between numeric types and widens integers. trunc[T](value) narrows an integer to a strictly narrower integer type. truncate is an accepted synonym for trunc. Both forms are expressions and both require the target type in square brackets.

const as_float = cast[f64](42)
const as_u64 = cast[u64](42)
const small = trunc[u8](300)
const back = cast[i32](small)

A cast between str and a numeric type is rejected; strings are not numbers. A narrowing cast without trunc is rejected even when the value would fit. This is deliberate: the spelling makes the possible loss of information visible at the call site.

Calls and Method Syntax#

A call names a function and supplies arguments in parentheses. A method call is syntactic sugar: the receiver is inserted as the first argument. This works for ordinary free functions as well as trait methods.

fn scale(value i32, factor i32) i32 => value * factor

fn main() {
    print(scale(3, 4))
    print((3).scale(4))
}

The two calls above are equivalent. Method syntax reads well when the first parameter is the subject of the operation, which is why collection functions and trait methods use it. It is only sugar; the receiver is not a special hidden parameter and there is no separate method table for free functions.

Field Access and Indexing#

A record exposes its fields with .. A list, string, or byte buffer supports indexing with [...]. The len property is available on lists (as i32), strings, and bytes (as u64).

type Point {
    x i32
    y i32
}

fn main() {
    const p = Point { x = 1, y = 2 }
    const xs = [10, 20, 30]
    print(p.x)
    print(xs.len)
    print(xs[1])
}

Indexing performs a runtime bounds check. Index assignment is permitted for lists and byte buffers; strings are immutable and their bytes cannot be assigned. The built-in get(list, index) returns Option[T] instead of failing, and it may be called with method syntax as list.get(index) when no user-defined get shadows it.

const xs = [10, 20, 30]
if xs.get(5) is Some(value) {
    print(value)
} else {
    print("out of range")
}

Conditional and Match Expressions#

An if with both branches is an expression whose type is the common type of the branches. A missing else makes it a statement of type void. A match is an expression when every arm produces a value and a statement when the arms produce none.

const label = if n > 0 { "positive" } else { "non-positive" }

const text = match value {
    Some(v) => $"found {v}"
    None => "missing"
}

All value-producing arms of a match must have compatible types. A match without a wildcard arm can fail at runtime if the value does not match any arm; see Pattern Matching.

Block Expressions#

Braces in expression position form a block expression. The value of the block is the value of its final expression, or void if the final statement does not produce a value. Declarations and statements inside the block are scoped to it.

const result = {
    const base = 10
    const extra = 5
    base + extra
}

A block expression is useful for keeping a temporary calculation local. Use it sparingly; a named function is usually clearer when the block grows beyond a few lines.

The ? Postfix#

The ? operator unwraps a Result when it is Success and returns the contained Error from the enclosing function when it is Error. It is also used after a record literal to request checked construction, as in Positive { n }?. Both uses are described in Option and Result and Checked Construction.

fn twice(value Result[i32, str]) Result[i32, str] {
    const n = value?
    return Success(n * 2)
}

The enclosing function must return a Result with a compatible error type. There is no unwrap and no implicit panic path for a failed Result.

Printing and Interpolation#

print accepts one value of a printable type: integers, floats, booleans, runes, and strings. Records, lists, sums, Option, and Result are not directly printable. Use interpolation to render components, or write an explicit formatting function.

print(42)
print(3.5)
print("text")
print($"point = {p.x}, {p.y}")