Stream: ideas

Topic: Tuple field setters


view this post on Zulip Derin Eryilmaz (Nov 08 2024 at 05:39):

I don't know how this would be expressed in the type system, but here goes:

What if we had &0, &1, etc as functions that set fields in tuples?

(1, 2, 3) |> &0 3 would be (3, 2, 3)

We have .field and &field syntax for records, but only .0 syntax for tuples, so it feels like adding this feature could fill that hole in the language. Also this would make it easier to modify large tuples (think matrix types) which there's no syntax for currently.

view this post on Zulip Derin Eryilmaz (Nov 08 2024 at 05:43):

Type of &0: (a)b, a -> (a)b
Type of &1: (a, b)c, b -> (a, b)c

It should be possible to express in the type system (although it could potentially have a lot of type parameters!)

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 16:02):

Did we ever add this feature for records? I think we should only consider this if it is already a record feature.

view this post on Zulip Derin Eryilmaz (Nov 08 2024 at 16:02):

We did, https://github.com/roc-lang/roc/pull/7002

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 16:03):

Oh cool. Missed that

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 16:04):

I'm currently neutral to the feature, but seems reasonable.

view this post on Zulip Sam Mohr (Nov 08 2024 at 19:46):

The main reason I like the idea is because it's orthogonal to our existing features

view this post on Zulip Joshua Warner (Nov 15 2024 at 02:48):

I'm struggling to think up possible real-world uses of this; do you have some in mind?

view this post on Zulip Derin Eryilmaz (Nov 15 2024 at 13:44):

for the same reason https://github.com/roc-lang/roc/pull/7002 exists: say you have an application state which is a tuple of 3 parts, and you want to pass a function that can set, say, the third state part. this would let you write &2 instead of having to write \(x, y, _), n -> (x, y, n). it would match the tuple getter of .2 and be consistent with the record syntax.

view this post on Zulip Derin Eryilmaz (Nov 15 2024 at 13:45):

assuming an app design like https://docs.google.com/document/d/16qY4NGVOHu8mvInVD-ddTajZYSsFvFBvQON_hmyHGfo/edit#heading=h.5gvhiqd8pauk


Last updated: Jun 16 2026 at 16:19 UTC