Tuples
A tuple groups two to four values whose types can differ.
Construction and Destructuring
Write (String, Int) for the type and ("Ada", 42) for a value. Bind its components together with (name, score) = player.
func main(args: List[String]) -> Void:
player: (String, Int) = ("Ada", 42)
(name, score) = player
print(name) -- prints: Ada
print(score.to_string()) -- prints: 42
print(player[0]) -- prints: Ada
print(player[1].to_string()) -- prints: 42
Indexing
Tuple indexes start at zero and must be in-range integer literals. Each position keeps its own type; an out-of-range index is a compiler error. Constant expressions such as 1 + 0 are not accepted as tuple indexes.
func invalid() -> Int:
player: (String, Int) = ("Ada", 42)
player[2]
Runtime Indexes
A runtime Int cannot select a tuple field: different fields need not have the same type. Destructure the tuple or use an integer literal instead.
func invalid(index: Int) -> Int:
values: (Int, Int) = (10, 20)
values[index]
Returning a Tuple
Return a tuple when a small result naturally has several parts. The caller can destructure it or select a field with an in-range integer literal.
pure func player_score() -> (String, Int):
("Ada", 42)
func main(args: List[String]) -> Void:
player: (String, Int) = player_score()
(name, score) = player
print(name) -- prints: Ada
print(score.to_string()) -- prints: 42
print(player[1].to_string()) -- prints: 42