Overview of Ada 2022
Jeff Cousins
Contents   Index   Search   Previous   Next 

3.4 Delta aggregates

Partial aggregate notation (AI12-0127) introduces a new syntactic form of aggregate, the delta aggregate (see RM 4.3.4). This allows one to update one or more fields of a composite object without having to specify every field. This will be particularly useful for postconditions, where one might want to check that only certain fields of a composite parameter had changed, for example:
procedure Twelfth (D : in out Date)
   with Post => D = (D'Old with delta Day => 12);
The values of the Year and Month components of the delta aggregate are the same as those of D'Old but the Day component is 12.
Delta aggregates always require the changed components to be written in the form of named associations. (Recall that an aggregate is made up of a list of associations; a named association includes an explicit specification of the component(s) to specify, the choose symbol (=>), and the expression giving the component value. For an array aggregate, the component specification is usually a discrete choice list, often referred to as the choices of the association.)
Unlike other aggregates, there is not a completeness check, as the whole point of a delta aggregate is to give some but not all components.
For record delta aggregates, most other rules are the same as for other record aggregates. Components can only be given once and are evaluated in an arbitrary order. A discriminant may not be specified as a component as to change a discriminant without changing its dependent components could prove disastrous. If a component is in a variant part, then a Discriminant_Check is performed as for the use of a component.
Array delta aggregates are more different than other array aggregates. There are a few limits on index expressions (the main one being that non-static expressions have to be the only one of an association, but multiple associations are allowed even when some are non-static). Unlike other aggregates, such as record delta aggregates, the components are evaluated in the order given. A component may be given multiple times, in which case the value from the last occurrence is the one used.
Delta aggregates can be used with target name symbols (see 7.7) to simplify setting several components at a time. For instance, consider the following Ada 2012 code to calculate basic statistical information for the parent of a particular tree node:
Node.Parent.Count  := Node.Parent.Count + 1;
Node.Parent.Sum    := Node.Parent.Sum + Value;
Node.Parent.Sq_Sum := Node.Parent.Sq_Sim + Value*Value;
In Ada 2022, one could instead write:
Node.Parent := (@ with delta
                           Count => @.Count + 1,
                           Sum => @.Sum + Value,
                           Sq_Sum => @.Sq_Sum + Value*Value);
This makes it clear that the intent is to update all of these components as a group without touching any other components (in particular, not changing the position of the node in the tree). It also is safer than the original code, as the delta aggregate does not allow setting the same component twice by mistake, and the target name symbol eliminates misspellings in the name of the node (see 7.7).

Contents   Index   Search   Previous   Next 
© 2021, 2022 Jeff Cousins