catala icon indicating copy to clipboard operation
catala copied to clipboard

Syntactic sugar for changing only part of a structure

Open AltGr opened this issue 1 year ago • 1 comments

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 avatar Mar 12 '24 11:03 AltGr

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

denismerigoux avatar Mar 12 '24 13:03 denismerigoux

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 avatar Apr 09 '24 14:04 AltGr

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

AltGr avatar Apr 11 '24 08:04 AltGr

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

denismerigoux avatar Apr 11 '24 15:04 denismerigoux