Ada Reference Manual (Ada 2022)Legal Information
Contents   Index   References   Search   Previous   Next 

3.3.1 Object Declarations

1/3
An object_declaration declares a stand-alone object with a given nominal subtype and, optionally, an explicit initial value given by an initialization expression. For an array, access, task, or protected object, the object_declaration may include the definition of the (anonymous) type of the object. 

Syntax

2/3
object_declaration ::= 
    defining_identifier_list : [aliased] [constantsubtype_indication [:= expression]
        [aspect_specification];
  | defining_identifier_list : [aliased] [constantaccess_definition [:= expression]
        [aspect_specification];
  | defining_identifier_list : [aliased] [constantarray_type_definition [:= expression]
        [aspect_specification];
  | single_task_declaration
  | single_protected_declaration
3
defining_identifier_list ::= 
  defining_identifier {, defining_identifier}

Name Resolution Rules

4
For an object_declaration with an expression following the compound delimiter :=, the type expected for the expression is that of the object. This expression is called the initialization expression.

Legality Rules

5/2
An object_declaration without the reserved word constant declares a variable object. If it has a subtype_indication or an array_type_definition that defines an indefinite subtype, then there shall be an initialization expression. 

Static Semantics

6/5
An object_declaration with the reserved word constant declares a constant object. If it has an initialization expression, then it is called a full constant declaration. Otherwise, it is called a deferred constant declaration. The rules for deferred constant declarations are given in 7.4. The rules for full constant declarations are given in this subclause.
7
Any declaration that includes a defining_identifier_list with more than one defining_identifier is equivalent to a series of declarations each containing one defining_identifier from the list, with the rest of the text of the declaration copied for each declaration in the series, in the same order as the list. The remainder of this Reference Manual relies on this equivalence; explanations are given for declarations with a single defining_identifier.
8/2
The subtype_indication, access_definition, or full type definition of an object_declaration defines the nominal subtype of the object. The object_declaration declares an object of the type of the nominal subtype. 
8.1/5
 A component of an object is said to require late initialization if: 
8.2/5
it has an access discriminant value constrained by a per-object expression; or
8.3/5
it has an initialization expression that includes a name denoting an access discriminant; or
8.4/5
it has an initialization expression that includes a reference to the current instance of the type either by name or implicitly as the target object of a call. 

Dynamic Semantics

9/2
If a composite object declared by an object_declaration has an unconstrained nominal subtype, then if this subtype is indefinite or the object is constant the actual subtype of this object is constrained. The constraint is determined by the bounds or discriminants (if any) of its initial value; the object is said to be constrained by its initial value. When not constrained by its initial value, the actual and nominal subtypes of the object are the same. If its actual subtype is constrained, the object is called a constrained object.
10
For an object_declaration without an initialization expression, any initial values for the object or its subcomponents are determined by the implicit initial values defined for its nominal subtype, as follows: 
11
The implicit initial value for an access subtype is the null value of the access type.
11.1/5
The implicit initial value for a scalar subtype that has the Default_Value aspect specified is the value of that aspect converted to the nominal subtype (which can raise Constraint_Error — see 4.6, “Type Conversions”);
12
The implicit initial (and only) value for each discriminant of a constrained discriminated subtype is defined by the subtype.
13/5
For a (definite) composite subtype, the implicit initial value of each component with a default_expression is obtained by evaluation of this expression and conversion to the component's nominal subtype (which can raise Constraint_Error), unless the component is a discriminant of a constrained subtype (the previous case), or is in an excluded variant (see 3.8.1). For each component that does not have a default_expression, if the composite subtype has the Default_Component_Value aspect specified, the implicit initial value is the value of that aspect converted to the component's nominal subtype; otherwise, any implicit initial values are those determined by the component's nominal subtype.
14
For a protected or task subtype, there is an implicit component (an entry queue) corresponding to each entry, with its implicit initial value being an empty queue. 
15
The elaboration of an object_declaration proceeds in the following sequence of steps: 
16/2
1.
The subtype_indication, access_definition, array_type_definition, single_task_declaration, or single_protected_declaration is first elaborated. This creates the nominal subtype (and the anonymous type in the last four cases).
17/5
2.
If the object_declaration includes an initialization expression, the (explicit) initial value is obtained by evaluating the expression and converting it to the nominal subtype (which can raise Constraint_Error — see 4.6).
18/2
3.
The object is created, and, if there is not an initialization expression, the object is initialized by default. When an object is initialized by default, any per-object constraints (see 3.8) are elaborated and any implicit initial values for the object or for its subcomponents are obtained as determined by the nominal subtype. Any initial values (whether explicit or implicit) are assigned to the object or to the corresponding subcomponents. As described in 5.2 and 7.6, Initialize and Adjust procedures can be called.
19/2
This paragraph was deleted.
20/2
For the third step above, evaluations and assignments are performed in an arbitrary order subject to the following restrictions: 
20.1/2
Assignment to any part of the object is preceded by the evaluation of the value that is to be assigned. 
20.2/2
The evaluation of a default_expression that includes the name of a discriminant is preceded by the assignment to that discriminant. 
20.3/2
The evaluation of the default_expression for any component that depends on a discriminant is preceded by the assignment to that discriminant. 
20.4/3
The assignments to any components, including implicit components, not requiring late initialization precede the initial value evaluations for any components requiring late initialization; if two components both require late initialization, then assignments to parts of the component occurring earlier in the order of the component declarations precede the initial value evaluations of the component occurring later.
21/5
There is no implicit initial value defined for a scalar subtype unless the Default_Value aspect has been specified for the type. In the absence of an explicit initialization or the specification of the Default_Value aspect, a newly created scalar object can have a value that does not belong to its subtype (see 13.9.1 and H.1). 
22
NOTE 1   Implicit initial values are not defined for an indefinite subtype, because if an object's nominal subtype is indefinite, an explicit initial value is required.
23/5
NOTE 2   As indicated above, a stand-alone object is an object declared by an object_declaration. Similar definitions apply to “stand-alone constant” and “stand-alone variable”. A subcomponent of an object is not a stand-alone object, nor is an object that is created by an allocator. An object declared by a loop_parameter_specification, iterator_specification, iterated_component_association, chunk_specification, parameter_specification, entry_index_specification, choice_parameter_specification, extended_return_statement, or a formal_object_declaration of mode in out is not considered a stand-alone object.
24
NOTE 3   The type of a stand-alone object cannot be abstract (see 3.9.3). 

Examples

25
Example of a multiple object declaration: 
26
-- the multiple object declaration
27/2
John, Paul : not null Person_Name := new Person(Sex => M); -- see 3.10.1
28
-- is equivalent to the two single object declarations in the order given
29/2
John : not null Person_Name := new Person(Sex => M);
Paul : not null Person_Name := new Person(Sex => M);
30
Examples of variable declarations: 
31/5
Count, Sum  : Integer;
Size        : Integer range 0 .. 10_000 := 0;
Sorted      : Boolean := False;
Color_Table : array(1 .. Max) of Color;
Option      : Bit_Vector(1 .. 10) := (others => True); -- see 3.6
Hello       : aliased String := "Hi, world.";
θ, φ        : Float range -π .. +π;
32
Examples of constant declarations: 
33/5
Limit     : constant Integer := 10_000;
Low_Limit : constant Integer := Limit/10;
Tolerance : constant Real := Dispersion(1.15);
A_String  : constant String := "A";
Hello_Msg : constant access String := Hello'Access; -- see 3.10.2

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe