Stream: ideas

Topic: Shorthand for record field setter


view this post on Zulip Jared Cone (Nov 08 2024 at 05:09):

There is syntax sugar for getting a field on a record: .field desugars to \x -> x.field. I was writing some code today where a setter version of that would be nice, maybe something like &field desugars to \field, x -> {x & field}. Useful for being able to pass a getter and setter to a function that can get a field, do some operation on it, then set it.

doSomething x.field (\field -> { x & field})
becomes
doSomething x .field &field

doSomething = \x, get, set -> get x |> someOperation |> set x

Minor savings I know, but helpful if you find yourself having to do this a lot.

view this post on Zulip Jared Cone (Nov 08 2024 at 05:13):

This example isn't much shorter than { x & field : doSomething x.field}, but setter functions can be passed around, saved for later use, etc

view this post on Zulip Luke Boswell (Nov 08 2024 at 05:20):

It's already implemented. Here's an example of using it https://github.com/lukewilliamboswell/roc-ray/blob/d69a1699d3e67e326bc9ed80ad0d2240c13f4d3d/examples/2d_camera.roc#L77

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

Wow! didn't know that

view this post on Zulip Luke Boswell (Nov 08 2024 at 05:21):

Yeah, things have been moving fast.

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

Can we add tuple setters? &0, &1 etc
or is the type system not powerful enough for that right now

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

i think that would help a lot for big tuples because there's no easy syntax to do that right now

view this post on Zulip Luke Boswell (Nov 08 2024 at 05:24):

Here was the implementation https://github.com/roc-lang/roc/pull/7002

view this post on Zulip Luke Boswell (Nov 08 2024 at 05:24):

For a separate Tuple accessor I think that's an interesting idea

view this post on Zulip Jared Cone (Nov 08 2024 at 05:24):

Nice!

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

Luke Boswell said:

For a separate Tuple accessor I think that's an interesting idea

Should i make a new channel for the idea or should it stay in here?

view this post on Zulip Sam Mohr (Nov 08 2024 at 05:35):

Let's make a new thread

view this post on Zulip Sam Mohr (Nov 08 2024 at 05:36):

Since it might be contentious, though I don't know


Last updated: Jun 16 2026 at 16:19 UTC