Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Record.replace method #505

Open
mtshiba opened this issue Mar 28, 2024 · 0 comments
Open

RFC: Record.replace method #505

mtshiba opened this issue Mar 28, 2024 · 0 comments
Labels
enhancement New feature or request RFC New specifications of Erg

Comments

@mtshiba
Copy link
Member

mtshiba commented Mar 28, 2024

If you want to replace only one element in a record, you can do it like:

rec = { x = 1; y = 2; z = 3 }
assert { x = rec.x; y = rec.y; z = 2 } == { x = 1; y = 2; z = 2 }

This is tedious and requires changing the code every time other fields are added or removed.
So, We propose to add a replace method to the record type.

rec = { x = 1; y = 2; z = 3 }
assert rec.replace({ z = 2 }) == { x = 1; y = 2; z = 2 }
assert rec.replace({ x = 2; z = 2 }) == { x = 2; y = 2; z = 2 }

The argument is a record containing the fields to be rewritten. If you provide a record containing a field that does not exist in self, a compilation error will occur.

Also, the replace method returns the same type of self. Therefore, all record field types given as an argument must match the field types of the original record.

@mtshiba mtshiba added enhancement New feature or request RFC New specifications of Erg labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request RFC New specifications of Erg
Projects
None yet
Development

No branches or pull requests

1 participant