Ada Reference Manual (Ada 2022) — Legal Information

# 4.1.6 User-Defined Indexing

#### Static Semantics

1/3
Given a tagged type T, the following type-related, operational aspects may be specified:
2/5
Constant_Indexing
This aspect shall be specified by a name that denotes one or more functions declared immediately within the same declaration list in which T, or the declaration completed by T, is declared. All such functions shall have at least two parameters, the first of which is of type T or T'Class, or is an access-to-constant parameter with designated type T or T'Class.
3/5
Variable_Indexing
This aspect shall be specified by a name that denotes one or more functions declared immediately within the same declaration list in which T, or the declaration completed by T, is declared. All such functions shall have at least two parameters, the first of which is of type T or T'Class, or is an access parameter with designated type T or T'Class. All such functions shall have a return type that is a reference type (see 4.1.5), whose reference discriminant is of an access-to-variable type.
4/4
These aspects are inherited by descendants of T (including the class-wide type T'Class).
5/3
An indexable container type is (a view of) a tagged type with at least one of the aspects Constant_Indexing or Variable_Indexing specified. An indexable container object is an object of an indexable container type. A generalized_indexing is a name that denotes the result of calling a function named by a Constant_Indexing or Variable_Indexing aspect.
5.1/4
The Constant_Indexing and Variable_Indexing aspects are nonoverridable (see 13.1.1).

#### Legality Rules

6/5
If an ancestor of a type T is an indexable container type, then any explicit specification of the Constant_Indexing or Variable_Indexing aspects shall be confirming; that is, the specified name shall match the inherited aspect (see 13.1.1).
Paragraphs 7 through 8 were deleted.
9/5
In addition to the places where Legality Rules normally apply (see 12.3), this rule applies also in the private part of an instance of a generic unit.
9.1/5
A generalized_indexing is illegal if the equivalent prefixed view (see below) is illegal.

#### Syntax

10/3
generalized_indexing ::= indexable_container_object_prefix actual_parameter_part

#### Name Resolution Rules

11/3
The expected type for the indexable_container_object_prefix of a generalized_indexing is any indexable container type.
12/3
If the Constant_Indexing aspect is specified for the type of the indexable_container_object_prefix of a generalized_indexing, then the generalized_indexing is interpreted as a constant indexing under the following circumstances:
13/3
when the Variable_Indexing aspect is not specified for the type of the indexable_container_object_prefix;
14/3
when the indexable_container_object_prefix denotes a constant;
15/3
when the generalized_indexing is used within a primary where a name denoting a constant is permitted.
16/3
Otherwise, the generalized_indexing is interpreted as a variable indexing.
17/3
When a generalized_indexing is interpreted as a constant (or variable) indexing, it is equivalent to a call on a prefixed view of one of the functions named by the Constant_Indexing (or Variable_Indexing) aspect of the type of the indexable_container_object_prefix with the given actual_parameter_part, and with the indexable_container_object_prefix as the prefix of the prefixed view.
18/4
NOTE The Constant_Indexing and Variable_Indexing aspects cannot be redefined when inherited for a derived type, but the functions that they denote can be modified by overriding or overloading.

#### Examples

19/5
Examples of the specification and use of generalized indexing:
20/3
type Indexed_Barrel is tagged ...
with Variable_Indexing => Find;
-- Indexed_Barrel is an indexable container type,
-- Find is the generalized indexing operation.
21/3
function Find (B : aliased in out Indexed_Barrel; Key : String)
return Ref_Element;
-- Return a reference to an element of a barrel (see 4.1.5).
22/3
IB: aliased Indexed_Barrel;
23/3
-- All of the following calls are then equivalent:
Find (IB,"pear").Data.all := Element'(...); -- Traditional call
IB.Find ("pear").Data.all := Element'(...); -- Call of prefixed view
IB.Find ("pear") := Element'(...); -- Implicit dereference (see 4.1.5)
IB ("pear") := Element'(...); -- Implicit indexing and dereference
IB ("pear").Data.all := Element'(...); -- Implicit indexing only

Ada 2005 and 2012 Editions sponsored in part by Ada-Europe