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

Syntactic sugar for changing only part of a structure #592

Closed
AltGr opened this issue Mar 12, 2024 · 4 comments · Fixed by #603
Closed

Syntactic sugar for changing only part of a structure #592

AltGr opened this issue Mar 12, 2024 · 4 comments · Fixed by #603
Labels
💬 discussion Further discussion is needed ✨ enhancement New feature or request 💡 language Language design

Comments

@AltGr
Copy link
Collaborator

AltGr commented Mar 12, 2024

Let's imagine the following code:

For purposes of this article, the income should be assumed to be at most $1200

and that we have a structure Person containing lots of data fields besides income: age, xx, yy,...

We would have to define the specific structure for the person as concerned by this article (supposing original_person is an input of the scope):

definition person_for_this_article equals
  let income equals (if original_person.income > $1200 then $1200 else original_person.income) in
  Person {
    -- income: income
    -- age: original_person.age
    -- xx: original_person.xx
    -- yy: original_person.yy
    etc.
  }

This is tedious to write, verbose, and coud introduce hard-to-spot errors. We propose a shorter syntax that would be equivalent to the code above:

definition person_for_this_article equals
  let income equals (if original_person.income > $1200 then $1200 else original_person.income) in
  Person { original_person amended with
    -- income: income
  }
  • It should be absolutely clear to the reader that Person { original_person amended with ... } is a new, distinct structure and that original_person is not altered in any way.
  • is the amended with keyword ok ? I am also unsure about the french equivalent (does "amendé par" work ?). Using just with (without amended), if it's as clear, would avoid the addition of a new keyword.
  • feel free to suggest other syntax options
@AltGr AltGr added ✨ enhancement New feature or request 💬 discussion Further discussion is needed 💡 language Language design labels Mar 12, 2024
@denismerigoux
Copy link
Contributor

The specific syntax for this definitely useful feature should be decided by our Syntax Committee (@slawsk and Liane) before the final implementation :)

@AltGr
Copy link
Collaborator Author

AltGr commented Apr 9, 2024

Results of the discussion with the syntax committee:

definition person_for_this_article equals
  let new_income equals (if original_person.income > $1200 then $1200 else original_person.income) in
  original_person but replace { -- income: new_income }

@AltGr
Copy link
Collaborator Author

AltGr commented Apr 11, 2024

@denismerigoux should the French version be "mais remplace", or "mais en remplaçant" which is a bit more verbpse but sounds better ?

@denismerigoux
Copy link
Contributor

mais en remplaçant is better. Don't forget to update the syntax cheat sheet :)

AltGr added a commit to AltGr/catala that referenced this issue Apr 11, 2024
Closes CatalaLang#592

A new node is added in `desugared`, and translated into an exploded structure
literal during translation to `scopelang`. The main reason to put it there is
that it needs to be after disambiguation, since that is used to discover the
type of the structure that is being updated.
AltGr added a commit to AltGr/catala that referenced this issue Apr 12, 2024
Closes CatalaLang#592

A new node is added in `desugared`, and translated into an exploded structure
literal during translation to `scopelang`. The main reason to put it there is
that it needs to be after disambiguation, since that is used to discover the
type of the structure that is being updated.
AltGr added a commit to AltGr/catala that referenced this issue Apr 12, 2024
Closes CatalaLang#592

A new node is added in `desugared`, and translated into an exploded structure
literal during translation to `scopelang`. The main reason to put it there is
that it needs to be after disambiguation, since that is used to discover the
type of the structure that is being updated.
denismerigoux pushed a commit to AltGr/catala that referenced this issue Apr 12, 2024
Closes CatalaLang#592

A new node is added in `desugared`, and translated into an exploded structure
literal during translation to `scopelang`. The main reason to put it there is
that it needs to be after disambiguation, since that is used to discover the
type of the structure that is being updated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💬 discussion Further discussion is needed ✨ enhancement New feature or request 💡 language Language design
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants