Stream: beginners

Topic: How to do Tuples


view this post on Zulip Luke Boswell (Mar 15 2023 at 04:08):

This might help... crates/compiler/test_gen/src/gen_tuples.rs

view this post on Zulip Luke Boswell (Mar 15 2023 at 04:10):

tuple = ("One", 0xAB01, 15_000_000i64)

thirdItem = tuple.2

expect thirdItem == 15_000_000i64

view this post on Zulip Luke Boswell (Mar 15 2023 at 04:22):

Added an issue 5141 for Tuples to be included in Tutorial

view this post on Zulip Notification Bot (Mar 15 2023 at 04:37):

3 messages were moved here from #beginners > Opaque types perf question by Luke Boswell.

view this post on Zulip Luke Boswell (Mar 15 2023 at 04:38):

I added an Issue 5143 for tracking Eq ability. Should tuples be comparable if all of the elements implement Eq?

view this post on Zulip Luke Boswell (Mar 15 2023 at 04:42):

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:

Definition

A tuple is defined using parentheses and commas. For example, myTuple: (1, "two", 3.0) is a tuple with three elements.

Destructuring

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

Indexing

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.

Size

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"

Comparison

expect (0.0, 2, "x") == (0.0, 2, "x") <-- doesn't seem to be working correctly

view this post on Zulip Brian Carroll (Mar 15 2023 at 06:09):

Tuples can have any number of elements except zero _or one_, I think!

view this post on Zulip Brian Carroll (Mar 15 2023 at 06:16):

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!

view this post on Zulip Brian Carroll (Mar 15 2023 at 06:16):

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!

view this post on Zulip Brian Carroll (Mar 15 2023 at 06:18):

In other languages I'd normally use them to return more than one value from a function.

view this post on Zulip Brian Carroll (Mar 15 2023 at 06:19):

Or in cases where there's no meaningful name

view this post on Zulip Brian Carroll (Mar 15 2023 at 06:22):

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.

view this post on Zulip Nick Hallstrom (Mar 15 2023 at 06:26):

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