Overview of Ada 2022
Jeff Cousins
Contents   Index   Search   Previous   Next 

4.3 Use subtype_indication in generalized iterators

Ada 2012 added the ability to simplify:
Vec : Int_Vectors.Vector;
...
for I in Vec.Iterate loop
   Vec(I) := Vec(I) + 1;
end loop;
to:
Vec : Int_Vectors.Vector;
...
for E : T of Vec loop
    E := E + 1;
end loop;
where the optional : T acts as a comment to the reader that the subtype of element E is T (and the compiler verifies this comment). Use subtype_indication in generalized iterators (AI12-0156) allows an optional subtype indication – though of the cursor not the element – to be given for the original in form of a for loop (see RM 5.5.2), i.e.:
for I : Index in Vec.Iterate loop
   Vec(I) := Vec(I) + 1;
end loop;
where Index is the subtype of the loop parameter.

Contents   Index   Search   Previous   Next 
© 2021, 2022 Jeff Cousins