Prev | Up | ____ | Back | Forward
TOC -- / --.-- / --.--.-- | Index | Search | Syntax | Help


4.3.3 Array Aggregates

(1)
In an array_aggregate, a value is specified for each component of an array, either positionally or by its index. For a positional_array_aggregate, the components are given in increasing-index order, with a final others, if any, representing any remaining components. For a named_array_aggregate, the components are identified by the values covered by the discrete_choices.
Syntax
(2)
       array_aggregate ::=
         positional_array_aggregate | named_array_aggregate
(3)
       positional_array_aggregate ::=
           (expression, expression {, expression})
         | (expression {, expression}, others => expression)
(4)
       named_array_aggregate ::=
           (array_component_association {, array_component_association})
(5)
       array_component_association ::=
           discrete_choice_list => expression
(6)
An n-dimensional array_aggregate is one that is written as n levels of nested array_aggregates (or at the bottom level, equivalent string_literals). For the multidimensional case (n >= 2) the array_aggregates (or equivalent string_literals) at the n-1 lower levels are called subaggregates of the enclosing n-dimensional array_aggregate. The expressions of the bottom level subaggregates (or of the array_aggregate itself if one-dimensional) are called the array component expressions of the enclosing n-dimensional array_aggregate.
Name Resolution Rules
(7)
The expected type for an array_aggregate (that is not a subaggregate) shall be a single nonlimited array type. The component type of this array type is the expected type for each array component expression of the array_aggregate.
(8)
The expected type for each discrete_choice in any discrete_choice_list of a named_array_aggregate is the type of the corresponding index; the corresponding index for an array_aggregate that is not a subaggregate is the first index of its type; for an (n-m)-dimensional subaggregate within an array_aggregate of an n-dimensional type, the corresponding index is the index in position m+1.
Legality Rules
(9)
An array_aggregate of an n-dimensional array type shall be written as an n-dimensional array_aggregate.
(10)
An others choice is allowed for an array_aggregate only if an applicable index constraint applies to the array_aggregate. An applicable index constraint is a constraint provided by certain contexts where an array_aggregate is permitted that can be used to determine the bounds of the array value specified by the aggregate. Each of the following contexts (and none other) defines an applicable index constraint:
(11)
(12)
(13)
(14)
(15)
(16)
The applicable index constraint applies to an array_aggregate that appears in such a context, as well as to any subaggregates thereof. In the case of an explicit_actual_parameter (or default_expression) for a call on a generic formal subprogram, no applicable index constraint is defined.
(17)
The discrete_choice_list of an array_component_association is allowed to have a discrete_choice that is a nonstatic expression or that is a discrete_range that defines a nonstatic or null range, only if it is the single discrete_choice of its discrete_choice_list, and there is only one array_component_association in the array_aggregate.
(18)
In a named_array_aggregate with more than one discrete_choice, no two discrete_choices are allowed to cover the same value (see 3.8.1); if there is no others choice, the discrete_choices taken together shall exactly cover a contiguous sequence of values of the corresponding index type.
(19)
A bottom level subaggregate of a multidimensional array_aggregate of a given array type is allowed to be a string_literal only if the component type of the array type is a character type; each character of such a string_literal shall correspond to a defining_character_literal of the component type.
Static Semantics
(20)
A subaggregate that is a string_literal is equivalent to one that is a positional_array_aggregate of the same length, with each expression being the character_literal for the corresponding character of the string_literal.
Dynamic Semantics
(21)
The evaluation of an array_aggregate of a given array type proceeds in two steps:
(22)
(23)
(24)
The bounds of the index range of an array_aggregate (including a subaggregate) are determined as follows:
(25)
(26)
(27)
(28)
For an array_aggregate, a check is made that the index range defined by its bounds is compatible with the corresponding index subtype.
(29)
For an array_aggregate with an others choice, a check is made that no expression is specified for an index value outside the bounds determined by the applicable index constraint.
(30)
For a multidimensional array_aggregate, a check is made that all subaggregates that correspond to the same index have the same bounds.
(31)
The exception Constraint_Error is raised if any of the above checks fail.

(32)
Examples
(33)
Examples of array aggregates with positional associations:
(34)
       (7, 9, 5, 1, 3, 2, 4, 8, 6, 0)
       Table'(5, 8, 4, 1, others => 0)  --  see 3.6
(35)
Examples of array aggregates with named associations:
(36)
       (1 .. 5 => (1 .. 8 => 0.0))      --  two-dimensional
       (1 .. N => new Cell)             --  N new cells, in particular for N = 0
(37)
       Table'(2 | 4 | 10 => 1, others => 0)
       Schedule'(Mon .. Fri => True,  others => False)  --  see 3.6
       Schedule'(Wed | Sun  => False, others => True)
       Vector'(1 => 2.5)                                --  single-component vector
(38)
Examples of two-dimensional array aggregates:
(39)
       -- Three aggregates for the same value of subtype Matrix(1..2,1..3) (see 3.6):
(40)
       ((1.1, 1.2, 1.3), (2.1, 2.2, 2.3))
       (1 => (1.1, 1.2, 1.3), 2 => (2.1, 2.2, 2.3))
       (1 => (1 => 1.1, 2 => 1.2, 3 => 1.3), 2 => (1 => 2.1, 2 => 2.2, 3 => 2.3))
(41)
Examples of aggregates as initial values:
(42)
       A : Table := (7, 9, 5, 1, 3, 2, 4, 8, 6, 0);        -- A(1)=7, A(10)=0
       B : Table := (2 | 4 | 10 => 1, others => 0);        -- B(1)=0, B(10)=1
       C : constant Matrix := (1 .. 5 => (1 .. 8 => 0.0)); -- C'Last(1)=5, C'Last(2)=8
(43)
       D : Bit_Vector(M .. N) := (M .. N => True);         -- see 3.6
       E : Bit_Vector(M .. N) := (others => True);
       F : String(1 .. 1) := (1 => 'F');  -- a one component aggregate: same as "F"

Prev | Up | ____ | Back | Forward
TOC -- / --.-- / --.--.-- | Index | Search | Syntax | Help

Ada WWW Home -- Email comments, additions, corrections, gripes, kudos, etc. to:

Magnus Kempe -- Magnus.Kempe@di.epfl.ch
Copyright statement
Page last generated: 95-03-12