A tuple is like a struct, as it holds multiple co-located values, each with its own type. Unlike a struct, neither the tuple itself, nor its elements, are named. Tuples match as equivalent if they have the same number of elements whose individual types match in the same order.
Tuples are particularly valuable for returning multiple values (without having to use a pre-defined struct). For example:
fn getxy(point Point) f32, f32: point.x, point.y fn main(point Point): imm x f32 imm y f32 x, y = getxy(point)
The getxy function returns a tuple holding two floating-point values. Notice that the tuple type is defined by a comma-separated list of types. Similarly, the tuple value itself is composed using comma-separated values. (Note: ) (Note: In some other contexts, such as a parameter or field tuple, it will be necessary to wrap comma-separated list of types or value within parentheses.)
When getxy is called by main, the two returned values are separately stored into the x and y variables. Notice the types of the tuple's elements match those of the variables used to store its values.
Another benefit lies with parallel assignment, where multiple values can be stored in different homes. For example, this example swaps the values of x and y.
x, y = y, x
Accessing a single element of a tuple
Similar to structs, the dot operator can be used to access a specific element of a tuple. Since elements don't have names (like fields), use an integer constant to specify which element to extract (with '0' as the first element):
y = getxy(point).1