[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.

The rules in this subclause are based on terms
and rules for discrete_choice_lists
defined in 3.8.1, “Variant
Parts and Discrete Choices”. For example,
the requirements that **others** come last and stand alone are found
there.

{*AI95-00287-01*}
positional_array_aggregate ::=

(expression, expression {, expression})

| (expression {, expression},**others** => expression)

| (expression {, expression},**others** => <>)

(expression, expression {, expression})

| (expression {, expression},

| (expression {, expression},

{*AI95-00287-01*}
array_component_association ::=

discrete_choice_list => expression

| discrete_choice_list => <>

discrete_choice_list => expression

| discrete_choice_list => <>

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 *subaggregate*s 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.

{*AI95-00287-01*}
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.

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.

An array_aggregate
of an n-dimensional array type shall be written as an n-dimensional array_aggregate.

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:

{*AI95-00318-02*}
For an explicit_actual_parameter,
an explicit_generic_actual_parameter,
the expression
of a return statement return_statement,
the initialization expression in an object_declaration,
or a default_expression
[(for a parameter or a component)], when the nominal subtype of the corresponding
formal parameter, generic formal parameter, function return
object result, object, or component
is a constrained array subtype, the applicable index constraint is the
constraint of the subtype;

For the expression
of an assignment_statement
where the name
denotes an array variable, the applicable index constraint is the constraint
of the array variable;

For the operand of a qualified_expression
whose subtype_mark
denotes a constrained array subtype, the applicable index constraint
is the constraint of the subtype;

For a component expression
in an aggregate,
if the component's nominal subtype is a constrained array subtype, the
applicable index constraint is the constraint of the subtype;

{*AI05-0147-1*}
For a parenthesized expression,
the applicable index constraint is that, if any, defined for the expression;.

{*AI05-0147-1*}
For a conditional_expression,
the applicable index constraint for each *dependent_*expression
is that, if any, defined for the conditional_expression.

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.

{*AI05-0153-3*}
The discrete_choice_list
of an array_component_association
is allowed to have a discrete_choice
that is a nonstatic choice_expression expression
or that is a subtype_indication
or range 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.

{*AI05-0262-1*}
In a named_array_aggregate
where all discrete_choices
are static 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.

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.

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.

The
evaluation of an array_aggregate
of a given array type proceeds in two steps:

1.

Any discrete_choices
of this aggregate and of its subaggregates are evaluated in an arbitrary
order, and converted to the corresponding index type;

2.

The array component expressions of the aggregate are evaluated in an
arbitrary order and their values are converted to the component subtype
of the array type; an array component expression is evaluated once for
each associated component.

{*AI05-0005-1*}
We don't need to say that <> is evaluated
once for each component, as <> means that each component is *initialized
by default*. That means that the actions defined for default initialization
are applied to each component individually. Initializing one component
by default and copying that to the others would be an incorrect implementation
in general (although it might be OK if the default initialization is
known to be constant).

{*AI95-00287-01*}
Each expression
in an array_component_association
defines the value for the associated component(s). For an array_component_association
with <>, the associated component(s) are initialized by default
as for a stand-alone object of the component subtype (see 3.3.1).

The
bounds of the index range of an array_aggregate
[(including a subaggregate)] are determined as follows:

For an array_aggregate
with an **others** choice, the bounds are those of the corresponding
index range from the applicable index constraint;

For a positional_array_aggregate
[(or equivalent string_literal)]
without an **others** choice, the lower bound is that of the corresponding
index range in the applicable index constraint, if defined, or that of
the corresponding index subtype, if not; in either case, the upper bound
is determined from the lower bound and the number of expressions
[(or the length of the string_literal)];

For a named_array_aggregate
without an **others** choice, the bounds are determined by the smallest
and largest index values covered by any discrete_choice_list.

For an array_aggregate,
a check is made that the index range defined by its bounds is compatible
with the corresponding index subtype.

{*AI05-0037-1*}
For an array_aggregate
with an **others** choice, a check is made that no expression or <> is specified for an index value outside the bounds
determined by the applicable index constraint.

For a multidimensional
array_aggregate,
a check is made that all subaggregates that correspond to the same index
have the same bounds.

NOTES

10 {*AI05-0004-1*}
In an array_aggregate,
positional notation may only be used with two or more expressions;
a single expression
in parentheses is interpreted as a parenthesized
expression parenthesized_expression.
A named_array_aggregate,
such as (1 => X), may be used to specify an array with a single component.

(1 .. 5 => (1 .. 8 => 0.0)) --* two-dimensional*

(1 .. N =>**new** Cell) --* N new cells, in particular for N = 0*

(1 .. N =>

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*

Schedule'(Mon .. Fri => True,

Schedule'(Wed | Sun => False,

Vector'(1 => 2.5) --

--* Three aggregates for the same value of subtype Matrix(1..2,1..3) (see 3.6):*

((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))

(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))

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*

B : Table := (2 | 4 | 10 => 1,

C :

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"*

E : Bit_Vector(M .. N) := (

F : String(1 .. 1) := (1 => 'F'); --

{*AI95-00433-01*}
*Example of an array aggregate with defaulted
others choice and with an applicable index constraint provided by an
enclosing record aggregate:*

Buffer'(Size => 50, Pos => 1, Value => String'('x', **others** => <>)) --* see 3.7*

In
Ada 95, no applicable index constraint is defined for a parameter in
a call to a generic formal subprogram; thus, some aggregates that are
legal in Ada 83 are illegal in Ada 95. For example:

...

I : constant Integer := F ((1 => '!', others => '?'));

--

This change eliminates
generic contract model problems.

We now allow "named
with others" aggregates in all contexts where there is an applicable
index constraint, effectively eliminating what was RM83-4.3.2(6). Sliding
never occurs on an aggregate with others, because its bounds come from
the applicable index constraint, and therefore already match the bounds
of the target.

The legality of an **others** choice is no
longer affected by the staticness of the applicable index constraint.
This substantially simplifies several rules, while being slightly more
flexible for the user. It obviates the rulings of AI83-00244 and AI83-00310,
while taking advantage of the dynamic nature of the "extra values"
check required by AI83-00309.

Named array aggregates are permitted even if
the index type is descended from a formal scalar type. See 4.9
and AI83-00190.

We now separate named and positional array aggregate
syntax, since, unlike other aggregates, named and positional associations
cannot be mixed in array aggregates (except that an **others** choice
is allowed in a positional array aggregate).

We have also reorganized the presentation to
handle multidimensional and one-dimensional aggregates more uniformly,
and to incorporate the rulings of AI83-00019, AI83-00309, etc.

{*AI95-00287-01*}
<> can be used in place
of an expression
in an array_aggregate,
default-initializing the component.

{*AI95-00287-01*}
Limited array_aggregates
are allowed (since all kinds of aggregates can now be limited, see 4.3).

{*AI95-00318-02*}
Fixed aggregates
to use the subtype of the return object of a function, rather than the
result subtype, because they can be different for an extended_return_statement,
and we want to use the subtype that's explicitly in the code at the point
of the expression.

{*AI05-0037-1*}
**Correction:** Fixed so
the check for components outside of the array applies to both expressions
and <>s. As <> was a new feature in Ada 2005, there should
be little existing code that depends on a <> component that is
specified outside of the array (and that is nonsense anyway, that a compiler
is likely to detect even without an explicit language rule disallowing
it).

{*AI05-0147-1*}
Added a definition of the applicable index constraint
for conditional_expressions
(which are new).

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