catala
catala copied to clipboard
Syntactic sugar for changing only part of a structure
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 thatoriginal_personis not altered in any way. - is the
amended withkeyword ok ? I am also unsure about the french equivalent (does "amendé par" work ?). Using justwith(withoutamended), if it's as clear, would avoid the addition of a new keyword. - feel free to suggest other syntax options
The specific syntax for this definitely useful feature should be decided by our Syntax Committee (@slawsk and Liane) before the final implementation :)
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 }
@denismerigoux should the French version be "mais remplace", or "mais en remplaçant" which is a bit more verbpse but sounds better ?
mais en remplaçant is better. Don't forget to update the syntax cheat sheet :)