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;

(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);

() is the Unit type:

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

Unit can be used as a value, 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);
}