Annotated Ada Reference ManualLegal Information
Contents   Index   References   Search   Previous   Next 

3.6.1 Index Constraints and Discrete Ranges

An index_constraint determines the range of possible values for every index of an array subtype, and thereby the corresponding array bounds. 


index_constraint ::=  (discrete_range {, discrete_range})
discrete_range ::= discrete_subtype_indication | range

Name Resolution Rules

{type of a discrete_range} The type of a discrete_range is the type of the subtype defined by the subtype_indication, or the type of the range. {expected type (index_constraint discrete_range) [partial]} For an index_constraint, each discrete_range shall resolve to be of the type of the corresponding index. 
Discussion: In Ada 95, index_constraints only appear in a subtype_indication; they no longer appear in constrained_array_definitions.

Legality Rules

An index_constraint shall appear only in a subtype_indication whose subtype_mark denotes either an unconstrained array subtype, or an unconstrained access subtype whose designated subtype is an unconstrained array subtype; in either case, the index_constraint shall provide a discrete_range for each index of the array type. 

Static Semantics

{bounds (of a discrete_range)} A discrete_range defines a range whose bounds are given by the range, or by the range of the subtype defined by the subtype_indication.

Dynamic Semantics

{compatibility (index constraint with a subtype) [partial]} An index_constraint is compatible with an unconstrained array subtype if and only if the index range defined by each discrete_range is compatible (see 3.5) with the corresponding index subtype. {null array} If any of the discrete_ranges defines a null range, any array thus constrained is a null array, having no components. {satisfies (an index constraint) [partial]} An array value satisfies an index_constraint if at each index position the array value and the index_constraint have the same index bounds. 
Ramification: There is no need to define compatibility with a constrained array subtype, because one is not allowed to constrain it again.
{elaboration (index_constraint) [partial]} The elaboration of an index_constraint consists of the evaluation of the discrete_range(s), in an arbitrary order. {evaluation (discrete_range) [partial]} The evaluation of a discrete_range consists of the elaboration of the subtype_indication or the evaluation of the range.
45  The elaboration of a subtype_indication consisting of a subtype_mark followed by an index_constraint checks the compatibility of the index_constraint with the subtype_mark (see 3.2.2).
46  Even if an array value does not satisfy the index constraint of an array subtype, Constraint_Error is not raised on conversion to the array subtype, so long as the length of each dimension of the array value and the array subtype match. See 4.6.


Examples of array declarations including an index constraint: 
Board     : Matrix(1 .. 8,  1 .. 8);  --  see 3.6
Rectangle : Matrix(1 .. 20, 1 .. 30);
Inverse   : Matrix(1 .. N,  1 .. N);  --  N need not be static 
Filter    : Bit_Vector(0 .. 31);
Example of array declaration with a constrained array subtype: 
My_Schedule : Schedule;  --  all arrays of type Schedule have the same bounds
Example of record type with a component that is an array: 
type Var_Line(Length : Natural) is
      Image : String(1 .. Length);
   end record;
Null_Line : Var_Line(0);  --  Null_Line.Image is a null array

Extensions to Ada 83

{extensions to Ada 83} We allow the declaration of a variable with a nominally unconstrained array subtype, so long as it has an initialization expression to determine its bounds. 

Wording Changes from Ada 83

We have moved the syntax for index_constraint and discrete_range here since they are no longer used in constrained_array_definitions. We therefore also no longer have to describe the (special) semantics of index_constraints and discrete_ranges that appear in constrained_array_definitions.
The rules given in RM83-3.6.1(5,7-10), which define the bounds of an array object, are redundant with rules given elsewhere, and so are not repeated here. RM83-3.6.1(6), which requires that the (nominal) subtype of an array variable be constrained, no longer applies, so long as the variable is explicitly initialized. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Sponsored by Ada-Europe