Skip to content

Tuples

Tuples group multiple values. Tuple types are written as (T1, T2, ...).

(Int, Int) split(Int value) {
    return (value, value + 1);
}

_ result = split(41);
print(result[0]);
print(result[1]);

(_ left, _ right) = split(41);

Destructuring bindings can choose mutability:

(_ x, _ y!) = split(41);
y = y + 1;

The parentheses introduce destructuring, including the one-binding form. By-value bindings keep a type position; borrowed bindings may omit it:

(Int left, _ right) = pair;
(ref borrowed) = value;
(Int[_] first, _[3] second) = arrays;

Tuple patterns use the same recursive shape in is, switch, enum payloads, and optional payloads. One payload tuple layer is supplied directly to the variant:

if node is .pair(left, right) { }
if node is .nested((left, right), tail) { }
if maybe_pair is .some(left, right) { }

(T) is equivalent to T:

(Int) add(Int a, Int b);
Int add(Int a, Int b);

(Int x!) = add(1, 2);
Int x! = add(1, 2);

Accordingly, (ref value) = source; borrows source directly; it does not project an index 0.

Void is the zero-size type; () is its only value:

Void hello() {
    print("Hello World!");
}

Void can be used as a parameter, local, and field type.

for in binds one pattern before in. Destructure tuples inside the loop body:

(Int, Int)[_] pairs = [(1, 2), (3, 4)];

for pair in pairs {
    (_ a, _ b) = pair;
    print(a + b);
}