Overview of Ada 2022
Jeff Cousins
Contents   Index   Search   Previous   Next 

7.7 @ as an abbreviation for the LHS of an assignment

The addition of the “target name symbol” by Add @ as an abbreviation for the LHS of an assignment (AI12-0125-3) (see RM 5.2.1) – the use of a single character placeholder for the left hand side of an assignment – has proved to be rather controversial with those who are used to Ada being verbose. However, one of the goals of Ada is to be readable, but having a lengthy name multiple times in the same statement and having to mentally determine whether occurrences were the same was not helping the readability of Ada code. In particular, the target name symbol can help make the intent of the code clearer. For instance, if you see:
My_Package.My_Array(I+1).Field :=
   My_Package.My_Array(I-1).Field + 1;
in Ada 2012, it's impossible to know if there is a “typo” here (with the I-1 intended to be I+1), or if something unusual is going on. Also, when in a hurry it would be very easy to assume that both of the names were the same (when they are not). If instead you see:
My_Package.My_Array(I+1).Field := @ + 1;
then the intent is clear, and there is much less chance of a typo.
This feature is similar in function to the += of the C family of languages. The Ada feature is more powerful though, being able to handle expressions such as series expansions. Here are a couple of examples:
My_Package.My_Array(I).Field :=
   My_Package.My_Array(I).Field ** 3 +
   My_Package.My_Array(I).Field ** 2 +
   My_Package.My_Array(I).Field;
could be shortened to:
My_Package.My_Array(I).Field := @ ** 3 + @ ** 2 + @;
and:
My_Package.My_Array(I).Field :=
   Natural'Min (My_Package.My_Array(I).Field, 1000);
could be shortened to:
My_Package.My_Array(I).Field := Natural'Min (@, 1000);
See 3.4 for another example of this kind.

Contents   Index   Search   Previous   Next 
© 2021, 2022 Jeff Cousins