Tuple
These will work similarly to how they do in Go and Python. While there is no tuple keyword, you can define tuples using the types used in them. For example:
int a, str b = (1, "Hi")
a == 1 // type int
b == "Hi" // type str
You can use this to return multiple values from a function, for example:
fn my_func(int a, int b) -> (int, int) {
int c = a + b
int d = a - b
return c, d
}
(int, int) vals = my_func(2, 4)
vals == (6, -2)
You can also access individual elements of a tuple with their index:
(int, str, float) vals = (1, "hi", 2.5)
vals[0] == 1
vals[1] == "hi"
vals[2] == 2.5
You can "destructure" a tuple by assigning its values to another tuple:
(int, int) vals = (1, 2)
int a, int b = vals
a == 1
b == 2
You can also partially destructure a tuple:
(int, int, int) vals = (1, 2, 3)
int a, (int, int) b = vals
a == 1
b == (2, 3)
You cannot destructure an optional tuple without first checking for none:
(int, int)? vals = none
if vals == none {
panic("Cannot destructure a none tuple")
}
(int, int)? vals = (3, 4)
if vals == none {
panic("Cannot destructure a none tuple")
}
// vals is now (int, int)
int a, int b = vals
assert(a == 3 && b == 4)