Go to the first, previous, next, last section, table of contents.


3.3.1 Object Declarations

  1. 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, task, or protected object, the object_declaration may include the definition of the (anonymous) type of the object.

    Syntax

  2. object_declaration ::=
        defining_identifier_list : [aliased] [constant]
          subtype_indication [:= expression];
      | defining_identifier_list : [aliased] [constant]
          array_type_definition [:= expression];
      | 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. 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. An initialization expression shall not be given if the object is of a limited type.

    Static Semantics

  6. 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 clause See section 7.4 Deferred Constants. 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 International Standard relies on this equivalence; explanations are given for declarations with a single defining_identifier.
  8. The subtype_indication 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.

    Dynamic Semantics

  9. 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 or aliased, See section 3.10 Access Types, 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. In the case of an aliased object, this initial value may be either explicit or implicit; in the other cases, an explicit initial value is required. 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:
    1. The implicit initial value for an access subtype is the null value of the access type.
    2. The implicit initial (and only) value for each discriminant of a constrained discriminated subtype is defined by the subtype.
    3. 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 might raise Constraint_Error -- See section 4.6 Type Conversions.), unless the component is a discriminant of a constrained subtype (the previous case), or is in an excluded variant, See section 3.8.1 Variant Parts and Discrete Choices. For each component that does not have a default_expression, any implicit initial values are those determined by the component's nominal subtype.
    4. 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.

  1. The elaboration of an object_declaration proceeds in the following sequence of steps:
    1. The subtype_indication, array_type_definition, single_task_declaration, or single_protected_declaration is first elaborated. This creates the nominal subtype (and the anonymous type in the latter three cases).
    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 might raise Constraint_Error -- See section 4.6 Type Conversions.).
    3. The object is created, and, if there is not an initialization expression, any per-object expressions, See section 3.8 Record Types are evaluated and any implicit initial values for the object or for its subcomponents are obtained as determined by the nominal subtype.
    4. Any initial values (whether explicit or implicit) are assigned to the object or to the corresponding subcomponents. As described in See section 5.2 Assignment Statements, and See section 7.6 User-Defined Assignment and Finalization, Initialize and Adjust procedures can be called.

  1. For the third step above, the object creation and any elaborations and evaluations are performed in an arbitrary order, except that if the default_expression for a discriminant is evaluated to obtain its initial value, then this evaluation is performed before that of the default_expression for any component that depends on the discriminant, and also before that of any default_expression that includes the name of the discriminant. The evaluations of the third step and the assignments of the fourth step are performed in an arbitrary order, except that each evaluation is performed before the resulting value is assigned.
  2. There is no implicit initial value defined for a scalar subtype. In the absence of an explicit initialization, a newly created scalar object might have a value that does not belong to its subtype See section 13.9.1 Data Validity, and See section H.1 Pragma Normalize_Scalars.

    NOTES

  3. (7) 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.
  4. (8) 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, parameter_specification, entry_index_specification, choice_parameter_specification, or a formal_object_declaration is not called a stand-alone object.
  5. (9) The type of a stand-alone object cannot be abstract, See section 3.9.3 Abstract Types and Subprograms.

    Examples

  6. Example of a multiple object declaration:
  7. --  the multiple object declaration
    
  8. John, Paul : Person_Name := new Person(Sex => M); -- See section 3.10.1 Incomplete Type Declarations
    
  9. --  is equivalent to the two single object
    --  declarations in the order given
    
  10. John : Person_Name := new Person(Sex => M);
    Paul : Person_Name := new Person(Sex => M);
    
  11. Examples of variable declarations:
  12. 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);
    Hello       : constant String := "Hi, world.";
    
  13. Examples of constant declarations:
  14. Limit     : constant Integer := 10_000;
    Low_Limit : constant Integer := Limit/10;
    Tolerance : constant Real := Dispersion(1.15);
    


Go to the first, previous, next, last section, table of contents.