4.3.3 Array Aggregates
Syntax
positional_array_aggregate ::=
(
expression,
expression {,
expression})
| (
expression {,
expression},
others =>
expression)
| (
expression {,
expression},
others => <>)
| '['
expression {,
expression}[,
others =>
expression] ']'
| '['
expression {,
expression},
others => <> ']'
null_array_aggregate ::= '[' ']'
Name Resolution Rules
The expected type for an
array_aggregate
(that is not a subaggregate) shall be a single array type.
The
component type of this array type is the expected type for each array
component expression of the
array_aggregate.
Legality Rules
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 that can be used to determine the bounds of the array
value specified by an
array_aggregate.
Each of the following contexts (and none other) defines an applicable
index constraint:
For an
explicit_actual_parameter,
an
explicit_generic_actual_parameter,
the
expression
of a return statement, the return expression of an expression function,
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, expression
function return object, object, or component is a constrained array subtype,
the applicable index constraint is the constraint of the subtype;
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;
For a parenthesized
expression,
the applicable index constraint is that, if any, defined for the
expression;
Static Semantics
The subtype (and nominal subtype) of an index parameter
is the corresponding index subtype.
Dynamic Semantics
1.
Each
iterator_specification
is elaborated (in an arbitrary order)
and an iteration
is performed solely to determine a maximum count for the number of values
produced by the iteration; all of these counts are combined to determine
the overall length of the array, and ultimately the limits on the bounds
of the array (defined below);
2.
A second iteration is performed for each of the
iterator_specifications,
in the order given in the
aggregate,
and for each value conditionally produced by the iteration (see
5.5
and
5.5.2), the associated
expression
is evaluated, its value is converted to the component subtype of the
array type, and used to define the value of the next component of the
array starting at the low bound and proceeding sequentially toward the
high bound. A check is made that the second iteration results in an array
length no greater than the maximum determined by the first iteration;
Constraint_Error is raised if this check
fails.
The
evaluation of any other
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.
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 to the Default_Component_Value
of the array type if this aspect has been specified for the array type;
otherwise, they 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 an
array_aggregate,
a check is made that the index range defined by its bounds is compatible
with the corresponding index subtype.
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.
The exception Constraint_Error
is raised if any of the above checks fail.
Implementation Permissions
NOTE 1 In an
array_aggregate
delimited by parentheses, positional notation can only be used with two
or more
expressions;
a single
expression
in parentheses is interpreted as a parenthesized expression. An
array_aggregate
delimited by square brackets can be used to specify an array with a single
component.
NOTE 2 An index parameter is a constant
object (see
3.3).
Examples
Examples of array
aggregates with positional associations:
(7, 9, 5, 1, 3, 2, 4, 8, 6, 0)
Table'(5, 8, 4, 1,
others => 0) --
see 3.6
Examples of array
aggregates with named associations:
(1 .. 5 => (1 .. 8 => 0.0)) -- two-dimensional
[1 .. N => new Cell] -- N new cells, in particular for N = 0
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
Examples of two-dimensional
array aggregates:
--
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)]
Examples of aggregates
as initial values:
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
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"
G : constant Matrix :=
(for I in 1 .. 4 =>
(for J in 1 .. 4 =>
(if I=J then 1.0 else 0.0))); -- Identity matrix
Empty_Matrix : constant Matrix := []; -- A matrix without elements
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 => ('x',
others => <>)) --
see 3.7
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe