Proposal: Overwrites on merging records

In the guide, merging records is described like this:

You can use an existing record to “fill in” the values of a new record:

{ a = 2, c = ~FF, ..g }
; g = { a = 1, b = "x", c = ~00 }
{ a = 2, b = "x", c = ~FF }

The value of a and c is kept from the original record. Wouldn’t it make more sense for g to overwrite values existing in both records? This would align more with mainstream languages and is arguably more useful.

To keep the existing behavior, one can just place ..g before values they don’t want to overwrite. This is exactly how it works in Javascript and is pretty logical:

{ ..g, a = 2, c = ~FF }
; g = { a = 1, b = "x", c = ~00 }
{ a = 2, b = "x", c = ~FF }

And:

{ a = 2, c = ~FF, ..g }
; g = { a = 1, b = "x", c = ~00 }
{ a = 1, b = "x", c = ~00 }
1 Like

I love it!

I’ll update the guide to match your example :cowboy_hat_face:

On second thought, we don’t have extensible objects like JS, and I’m unsure how hard they would be to implement.

Without extensible objects, it never makes sense to put ..g at the end of a record. I think we should restrict the ..g element to the beginning of the record so that we have flexibility for this later.

So, for now, this should be valid:

{ ..g, a = 2, c = ~FF }

And this is not:

{ a = 2, c = ~FF, ..g }

The latter case could still be allowed if g is a “subset” i.e. only differs by 0 or more missing fields. But I understand how that’s a weird thing to enforce for users.