Skip to content

Guide 24 of 53

Fixed arrays and const generic sizes

Check a packet's exact length and infer that length in reusable functions.

A fixed array records its length in its type. Use it when a packet, coordinate, or batch must contain an exact number of elements.

struct Buffer<const N: int> {
    bytes: [int; N]
}

fn sum<const N: int>(samples: [int; N]) -> int {
    let total = 0
    for sample in samples {
        total += sample
    }
    total
}

let packet: Buffer<3> = Buffer { bytes: [12, 18, 30] }
print(sum(packet.bytes))

The checker verifies the literal has three integers. The function receives its size from the argument; inside a const generic function, N is an immutable integer value as well as an array length. Calls through an inferred function alias or a stored callable retain this contract. Default arguments are filled before the compiler supplies the size.

Lengths accept nonnegative integer literals and module-level integer constants. Static arithmetic follows ordinary Zolo integer semantics: 2 + 1, 7 ~/ 2, and -1 % 3 produce length 3, 3, and 2. The / operator produces a float and cannot describe an array length. Overflow, division by zero, a runtime binding, and symbolic arithmetic such as N + 1 receive a diagnostic.

Fixed arrays support reads, iteration, and operations that do not change their length. Replacing a slot is checked when its index is a known integer inside the array's bounds. A fixed array cannot become a mutable dynamic array or Any through an annotation, cast, callable adapter, or an unconstrained generic parameter. Write an explicit [T; N] formal when passing a fixed array to a helper.

let point: [int; 2] = [4, 7]
point[0] = 5
print(point[1])

A concrete fixed array field with a default receives fresh elements for each construction. A const generic nominal type needs explicit fields or a factory that takes a fixed array; automatic Default and field default expressions cannot yet carry its size. Const parameters must occur in the type's layout: phantom const nominal identity is not supported.

An owner size in impl Buffer<N> is available in type annotations. To use a size as a runtime value or forward it to another const generic function, declare a const parameter on that function. A const generic function cannot use variadic arguments, source @inline, or a parameter that shadows its size. Reification applies to type parameters rather than integer const parameters.

Fixed arrays are represented by the existing array carrier. The frontend preserves the length proof, and schema guards verify both its element types and its exact length. Foreign ABI adapters and CLI @arg parsing currently report fixed arrays as unsupported instead of dropping the length.

DOCS / FEEDBACK

Did this page leave a question?

Tell us where the explanation lost you. Documentation is part of the language experience.

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br