This might help... crates/compiler/test_gen/src/gen_tuples.rs
tuple = ("One", 0xAB01, 15_000_000i64)
thirdItem = tuple.2
expect thirdItem == 15_000_000i64
Added an issue 5141 for Tuples to be included in Tutorial
3 messages were moved here from #beginners > Opaque types perf question by Luke Boswell.
I added an Issue 5143 for tracking Eq
ability. Should tuples be comparable if all of the elements implement Eq
?
Some draft notes below on Tuples... for the Tutorial. Please let me know if this is correct or if I'm missing something important.
Tuples are an ordered collection of elements to group related data. Tuples are often used to represent fixed collections of related data, such as the coordinates of a point or the RGB values of a color. Here are some important things to know about using tuples:
A tuple is defined using parentheses and commas. For example, myTuple: (1, "two", 3.0)
is a tuple with three elements.
a = (2, 4)
b = (5, 6)
f : (I64, I64)a, (I64, I64)b -> I64
f = \(x1, x2), (x3, x4) -> x1 + x2 + x3 + x4
expect f a b == 17
You can access individual elements of a tuple using .
to access values, starting at 0
. For example the expression (1, "two", 3.0).1
evaluates to "two" : Str
.
Tuples can have any number of elements, except zero. Tuples can also be nested, meaning that they can contain other tuples as elements. For example;
nestedTuple = (2, ("Roc", 0.0))
expect nestedTuple.1.0 == "Roc"
expect (0.0, 2, "x") == (0.0, 2, "x")
<-- doesn't seem to be working correctly
Tuples can have any number of elements except zero _or one_, I think!
I think we should have a section that describes when not to use them. There was quite a lot of debate about whether we should even have them in the language!
The trouble with tuples is that the fields are anonymous. Records have named fields which is usually better. RGB is actually a great example of that! I would say always use records for RGB and usually for points!
In other languages I'd normally use them to return more than one value from a function.
Or in cases where there's no meaningful name
I can't remember the motivating examples that finally led us to put tuples in Roc. I know SIMD support was one, but that's not a good example because I don't think we've implemented the rest of SIMD! I can't remember the other use cases.
Ya I’d love to see some example of when to use each of records, tuples, and tags. They all seem to have a good amount of overlap and it seems like you could actually use all 3 for the same job sometimes which I’m sure will be a bit confusing to beginners.
Last updated: Jul 06 2025 at 12:14 UTC