Ada Conformity Assessment Authority      Home Conformity Assessment   Test Suite ARGAda Standard
 
Ada Reference Manual (Ada 202x Draft 14)Legal Information
Contents   Index   References   Search   Previous   Next 

5.5.3 Procedural Iterators

1/5
A procedural_iterator invokes a user-defined procedure, passing in the body of the enclosing loop_statement as a parameter of an anonymous access-to-procedure type, to allow the loop body to be executed repeatedly as part of the invocation of the user-defined procedure. 

Syntax

2/5
procedural_iterator ::= 
     iterator_parameter_specification of iterator_procedure_call
3/5
iterator_parameter_specification ::= 
     formal_part
   | (identifier{, identifier})
4/5
iterator_procedure_call ::= 
     procedure_name
   | procedure_prefix iterator_actual_parameter_part
5/5
iterator_actual_parameter_part ::= 
     (iterator_parameter_association {, iterator_parameter_association})
6/5
iterator_parameter_association ::= 
     parameter_association
   | parameter_association_with_box
7/5
parameter_association_with_box ::= 
   [ formal_parameter_selector_name => ] <>
8/5
At most one iterator_parameter_association within an iterator_actual_parameter_part shall be a parameter_association_with_box.

Name Resolution Rules

9/5
The name or prefix given in an iterator_procedure_call shall resolve to denote a callable entity C that is a procedure, or an entry renamed as (viewed as) a procedure. The name or prefix shall not resolve to denote an abstract subprogram unless it is also a dispatching subprogram. When there is an iterator_actual_parameter_part, the prefix can be an implicit_dereference of an access-to-subprogram value.
10/5
An iterator_procedure_call without a parameter_association_with_box is equivalent to one with an iterator_actual_parameter_part with an additional parameter_association_with_box at the end, with the formal_parameter_selector_name identifying the last formal parameter of the callable entity denoted by the name or prefix.
11/5
An iterator_procedure_call shall contain at most one iterator_parameter_association for each formal parameter of the callable entity C. Each formal parameter without an iterator_parameter_association shall have a default_expression (in the profile of the view of C denoted by the name or prefix). This rule is an overloading rule (see 8.6).
12/5
The formal parameter of the callable entity C associated with the parameter_association_with_box shall be of an anonymous access-to-procedure type A

Legality Rules

13/5
The anonymous access-to-procedure type A shall have at least one formal parameter in its parameter profile. If the iterator_parameter_specification is a formal_part, then this formal_part shall be mode conformant with that of A. If the iterator_parameter_specification is a list of identifiers, the number of formal parameters of A shall be the same as the length of this list.

Static Semantics

14/5
A loop_statement with an iteration_scheme that has a procedural_iterator is equivalent to a local declaration of a procedure P followed by a procedure_call_statement that is formed from the iterator_procedure_call by replacing the <> of the parameter_association_with_box with P'Access. The formal_part of the locally declared procedure P is formed from the formal_part of the anonymous access-to-procedure type A, by replacing the identifier of each formal parameter of this formal_part with the identifier of the corresponding formal parameter or element of the list of identifiers given in the iterator_parameter_specification.
15/5
The following aspect may be specified for a subprogram or entry S that has at least one formal parameter of an anonymous access-to-subprogram type:
16/5
Allows_Exit
The Allows_Exit aspect is of type Boolean. The specified value shall be static. The Allows_Exit of an inherited primitive subprogram is True if Allows_Exit is True either for the corresponding subprogram of the progenitor type or for any other inherited subprogram that it overrides. If not specified or inherited as True, the Allows_Exit aspect of a subprogram or entry is False.
17/5
Specifying the Allows_Exit aspect True for a subprogram asserts that the subprogram is prepared to be completed by arbitrary transfers of control from the subprogram represented by the access-to-subprogram value, including propagation of exceptions. A subprogram for which Allows_Exit is True should use finalization for any necessary cleanup, and in particular should not use exception handling to implement cleanup.

Legality Rules

18/5
If a subprogram or entry overrides an inherited dispatching subprogram that has a True Allows_Exit aspect, only a confirming specification of True is permitted for the aspect on the overriding declaration.
19/5
The sequence_of_statements of a loop_statement with a procedural_iterator as its iteration_scheme shall contain an exit_statement, return statement, goto_statement, or requeue_statement that leaves the loop only if the callable entity C associated with the procedural_iterator has an Allows_Exit aspect specified True.

Examples

20/5
Example of iterating over a map from My_Key_Type to My_Element_Type (see A.18.4):
21/5
for (C : Cursor) of My_Map.Iterate loop
   Put_Line (My_Key_Type'Image (Key (C)) & " => " &
      My_Element_Type'Image (Element (C)));
end loop;
22/5
-- The above is equivalent to:
23/5
declare
   procedure P (C : Cursor) is
   begin
      Put_Line (My_Key_Type'Image (Key (c)) & " => " &
         My_Element_Type'Image (Element (C)));
   end P;
begin
   My_Map.Iterator (P'Access);
end;
24/5
Example of iterating over the environment variables (see A.17):
25/5
for (Name, Val) of Ada.Environment_Variables.Iterate(<>) loop
   --  "(<>)" is optional because it is the last parameter
   Put_Line (Name & " => " & Val);
end loop;
26/5
-- The above is equivalent to:
27/5
declare
   procedure P (Name : String; Val : String) is
   begin
      Put_Line (Name & " => " & Val);
   end P;
begin
   Ada.Environment_Variables.Iterate (P'Access);
end;

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe