Aspect Iterable (GNAT Reference Manual) (original) (raw)
3.28 Aspect Iterable ¶
This aspect provides a light-weight mechanism for loops and quantified expressions over container types, without the overhead imposed by the tampering checks of standard Ada 2012 iterators. The value of the aspect is an aggregate with six named components, of which the last three are optional: First,Next, Has_Element, Element, Last, and Previous. When only the first three components are specified, only thefor .. in form of iteration over cursors is available. When Elementis specified, both this form and the for .. of form of iteration over elements are available. If the last two components are specified, reverse iterations over the container can be specified (analogous to what can be done over predefined containers that support the Reverse_Iterator interface). The following is a typical example of use:
type List is private with Iterable => (First => First_Cursor, Next => Advance, Has_Element => Cursor_Has_Element [,Element => Get_Element] [,Last => Last_Cursor] [,Previous => Retreat]);
- The values of
FirstandLastare primitive operations of the container type that return aCursor, which must be a type declared in the container package or visible from it. For example:
function First_Cursor (Cont : Container) return Cursor; function Last_Cursor (Cont : Container) return Cursor;
- The values of
NextandPreviousare primitive operations of the container type that take both a container and a cursor and yield a cursor. For example:
function Advance (Cont : Container; Position : Cursor) return Cursor; function Retreat (Cont : Container; Position : Cursor) return Cursor;
- The value of
Has_Elementis a primitive operation of the container type that takes both a container and a cursor and yields a boolean. For example:
function Cursor_Has_Element (Cont : Container; Position : Cursor) return Boolean;
- The value of
Elementis a primitive operation of the container type that takes both a container and a cursor and yields anElement_Type, which must be a type declared in the container package or visible from it. For example:
function Get_Element (Cont : Container; Position : Cursor) return Element_Type;
This aspect is used in the GNAT-defined formal container packages.