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?
we should give Utc
the Encoding
ability
For now, you have to type out all of the fields, yes.
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
In general, we should push for all custom types in the future to have as many of the generated methods as possible
In our stdlib/platforms
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.
But I get your point.
I think the door is still open to implementing it
#contributing > record `..` update syntax @ 💬
Here's the most recent discussion
Feel free to suggest against the conclusion we made in there
Last updated: Jul 26 2025 at 12:14 UTC