Entomy / Ada-Improvements

Repository of Ada language improvement ideas
GNU General Public License v3.0
4 stars 0 forks source link

Anonymous constraints #27

Open Entomy opened 4 years ago

Entomy commented 4 years ago

Should Support:

function Discount(Cost : Currency, Rate : Integer range 0 .. 100) return Currency;

And similar

Rationale: Sometimes, especially in mathematics, a function is valid only for a specific domain. These cases, especially 0.0 .. 1.0, really don't justify their own type, especially since Ada is nominatively typed. Not having this either requires adding this named type and having downstream convert to this type, or use more common types and add validation code inside your function like what is done with other languages, deferring errors to execution time instead.

Neither of these options are great. Interestingly enough these anonymous constraints are allowed in plenty of other cases, such as record fields and most dumbfoundedly, the return type of a function.

This is clearly an oversight and should be resolved.