Stream: beginners

Topic: Convert one field of a record


view this post on Zulip Oskar Hahn (Jan 08 2025 at 19:16):

I have a record with many fields. One of the fields has the type Utc.Utc from basic-webserver.

To convert the record to json, I have to convert the field to something else that implements encodable. What I want to do is something likeencodable_object = {object & time: object.time |> Utc.to_nanos_since_epoch}. But this does not work, since the type of { object & ...} has to have the same type as object. So you can change the value, but not the type.

Is there an easy way to create a new record with a similar type? Or do I have to type out all fields?

view this post on Zulip Richard Feldman (Jan 08 2025 at 19:17):

we should give Utc the Encoding ability

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:17):

For now, you have to type out all of the fields, yes.

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:18):

We didn't want to enable using record updates to change the underlying type because that would make it really easy for beginners to require moving a lot of data around

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:18):

In general, we should push for all custom types in the future to have as many of the generated methods as possible

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:18):

In our stdlib/platforms

view this post on Zulip Oskar Hahn (Jan 08 2025 at 19:20):

Sam Mohr said:

We didn't want to enable using record updates to change the underlying type because that would make it really easy for beginners to require moving a lot of data around

Only if the ref count is higher then 1 (which is true for everything in Roc) and the new type has a different size or alignment then the old type.

It does not seem so different then List.map. It also copies a lot of memory if the new size is different.

view this post on Zulip Oskar Hahn (Jan 08 2025 at 19:21):

But I get your point.

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:21):

I think the door is still open to implementing it

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:24):

#contributing > record `..` update syntax @ 💬

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:24):

Here's the most recent discussion

view this post on Zulip Sam Mohr (Jan 08 2025 at 19:24):

Feel free to suggest against the conclusion we made in there


Last updated: Jul 26 2025 at 12:14 UTC