Stream: beginners

Topic: Why no syntax for extending records


view this post on Zulip Henrik Larsson (Mar 18 2025 at 18:11):

One thing I find strange that Roc does not have is a way to extend a record.

For example:

get_token! : {key: Str} => {key: Str, token: Str}
get_token! = |conf|
    token = get!(conf.key)
    {conf  extend session: token}

This is a pattern I use all the time in Clojure and Javascript to build up more complex structures. Is there a reason we only have the & in Roc to change values in a record but not extending a record?

view this post on Zulip Anton (Mar 18 2025 at 18:19):

I think we chose not to support extending like this because it could be really bad for performance. The old record has to be copied over entirely to a new spot in memory.

view this post on Zulip Henrik Larsson (Mar 18 2025 at 18:27):

Ok good to know the reason but its to bad, I really love that pattern in untyped languages paired with the pipe syntax.

view this post on Zulip Anton (Mar 18 2025 at 18:36):

Yeah, I think the recommended alternative would be to use a tag union: [NoToken, Token(Str)]

view this post on Zulip Brendan Hansknecht (Mar 18 2025 at 21:19):

As a note, we plan to support extending records in the future, but it is pretty low on the backlog.


Last updated: Jul 06 2025 at 12:14 UTC