5.5.2 Generalized Loop Iteration
Syntax
Name Resolution Rules
Glossary entry: An
iterator is a construct that is used to loop over the elements of an
array or container. Iterators may be user defined, and may perform arbitrary
computations to access elements from a container.
Legality Rules
{
AI05-0139-2}
If the reserved word reverse appears, the
iterator_specification
is a reverse iterator;
otherwise it is a forward iterator.
In a reverse generalized iterator, the iterator_name
shall be of a reversible iterator type. In a reverse container element
iterator, the default iterator type for the type of the iterable_name
shall be a reversible iterator type.
{
AI05-0139-2}
The type of the subtype_indication,
if any, of an array component iterator shall cover the component type
of the type of the iterable_name.
The type of the subtype_indication,
if any, of a container element iterator shall cover the default element
type for the type of the iterable_name.
{
AI05-0139-2}
In a container element iterator whose iterable_name
has type T, if the iterable_name
denotes a constant or the Variable_Indexing aspect is not specified for
T, then the Constant_Indexing aspect shall be specified for T.
Static Semantics
{
AI05-0139-2}
{
AI05-0269-1}
{
AI05-0292-1}
An iterator_specification
declares a loop parameter. In a generalized
iterator, the nominal subtype of the loop parameter is the iteration
cursor subtype. In an array component iterator or a container element
iterator, if a subtype_indication
is present, it determines the nominal subtype of the loop parameter.
In an array component iterator, if a subtype_indication
is not present, the nominal subtype of the loop parameter is the component
subtype of the type of the iterable_name.
In a container element iterator, if a subtype_indication
is not present, the nominal subtype of the loop parameter is the default
element subtype for the type of the iterable_name.
{
AI05-0139-2}
{
AI05-0292-1}
In a generalized iterator, the loop parameter is
a constant. In an array component iterator, the loop parameter is a constant
if the iterable_name
denotes a constant; otherwise it denotes a variable. In a container element
iterator, the loop parameter is a constant if the iterable_name
denotes a constant, or if the Variable_Indexing aspect is not specified
for the type of the iterable_name;
otherwise it is a variable.
Dynamic Semantics
{
AI05-0139-2}
For a generalized iterator, the loop parameter
is created, the iterator_name
is evaluated, and the denoted iterator object becomes the loop iterator.
In a forward generalized iterator, the operation First of the iterator
type is called on the loop iterator, to produce the initial value for
the loop parameter. If the result of calling Has_Element on the initial
value is False, then the execution of the loop_statement
is complete. Otherwise, the sequence_of_statements
is executed and then the Next operation of the iterator type is called
with the loop iterator and the current value of the loop parameter to
produce the next value to be assigned to the loop parameter. This repeats
until the result of calling Has_Element on the loop parameter is False,
or the loop is left as a consequence of a transfer of control. For a
reverse generalized iterator, the operations Last and Previous are called
rather than First and Next.
{
AI05-0139-2}
{
AI05-0292-1}
For an array component iterator, the iterable_name
is evaluated and the denoted array object becomes the array for the
loop. If the array for the loop is a null array,
then the execution of the loop_statement
is complete. Otherwise, the sequence_of_statements
is executed with the loop parameter denoting each component of the array
for the loop, using a canonical order of components,
which is last dimension varying fastest (unless the array has convention
Fortran, in which case it is first dimension varying fastest). For a
forward array component iterator, the iteration starts with the component
whose index values are each the first in their index range, and continues
in the canonical order. For a reverse array component iterator, the iteration
starts with the component whose index values are each the last in their
index range, and continues in the reverse of the canonical order. The
loop iteration proceeds until the sequence_of_statements
has been executed for each component of the array for the loop, or until
the loop is left as a consequence of a transfer of control.
{
AI05-0139-2}
{
AI05-0292-1}
For a container element iterator, the iterable_name
is evaluated and the denoted iterable container object becomes the iterable
container object for the loop. The default iterator
function for the type of the iterable container object for the loop is
called on the iterable container object and the result is the loop
iterator. An object of the default cursor subtype
is created (the loop cursor).
{
AI05-0139-2}
{
AI05-0292-1}
For a forward container element iterator, the operation
First of the iterator type is called on the loop iterator, to produce
the initial value for the loop cursor. If the result of calling Has_Element
on the initial value is False, then the execution of the loop_statement
is complete. Otherwise, the sequence_of_statements
is executed with the loop parameter denoting an indexing (see 4.1.6)
into the iterable container object for the loop, with the only parameter
to the indexing being the current value of the loop cursor; then the
Next operation of the iterator type is called with the loop iterator
and the loop cursor to produce the next value to be assigned to the loop
cursor. This repeats until the result of calling Has_Element on the loop
cursor is False, or until the loop is left as a consequence of a transfer
of control. For a reverse container element iterator, the operations
Last and Previous are called rather than First and Next. If the loop
parameter is a constant (see above), then the indexing uses the default
constant indexing function for the type of the iterable container object
for the loop; otherwise it uses the default variable indexing function.
Examples
{
AI05-0269-1}
-- Array component iterator example:
for Element of Board loop -- See 3.6.1.
Element := Element * 2.0; -- Double each element of Board, a two-dimensional array.
end loop;
Extensions to Ada 2005
{
AI05-0139-2}
Generalized forms of loop iteration
are new.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe