3.2. Objects and Named Numbers

[PREVIOUS][UP][NEXT]

An object is an entity that contains (has) a value of a given type. An object is one of the following:

A number declaration is a special form of object declaration that associates an identifier with a value of type universal_integer or universal_real.

    object_declaration ::=
         identifier_list : [constant] subtype_indication [:= expression];
       | identifier_list : [constant] constrained_array_definition [:= expression]; 

    number_declaration ::=
         identifier_list : constant := universal_static_expression; 

    identifier_list ::=  identifier {, identifier}                                             

An object declaration is called a single object declaration if its identifier list has a single identifier; it is called a multiple object declaration if the identifier list has two or more identifiers. A multiple object declaration is equivalent to a sequence of the corresponding number of single object declarations. For each identifier of the list, the equivalent sequence has a single object declaration formed by this identifier, followed by a colon and by whatever appears at the right of the colon in the multiple object declaration; the equivalent sequence is in the same order as the identifier list.

A similar equivalence applies also for the identifier lists of number declarations, component declarations, discriminant specifications, parameter specifications, generic parameter declarations, exception declarations, and deferred constant declarations.

In the remainder of this reference manual, explanations are given for declarations with a single identifier; the corresponding explanations for declarations with several identifiers follow from the equivalence stated above.

Example:

    --  the multiple object declaration 

    JOHN, PAUL : PERSON_NAME := new PERSON(SEX => M);  --  see 3.8.1 

    --  is  equivalent  to  the two single object declarations in the order
        given

    JOHN : PERSON_NAME := new PERSON(SEX => M);
    PAUL : PERSON_NAME := new PERSON(SEX => M); 

References: access type, constrained array definition, component, declaration, deferred constant declaration, designate, discriminant specification, entry, exception declaration, expression, formal parameter, generic formal object, generic parameter declaration, generic unit, generic subprogram, identifier, loop parameter, numeric type, parameter specification, scope, simple name, single task declaration, slice, static expression, subprogram, subtype indication, type, universal_integer type, universal_real type.

Sub-topics:

3.2.1. Object Declarations

[UP][NEXT]

An object declaration declares an object whose type is given either by a subtype indication or by a constrained array definition. If the object declaration includes the assignment compound delimiter followed by an expression, the expression specifies an initial value for the declared object; the type of the expression must be that of the object.

The declared object is a constant if the reserved word constant appears in the object declaration; the declaration must then include an explicit initialization. The value of a constant cannot be modified after initialization. Formal parameters of mode in of subprograms and entries, and generic formal parameters of mode in, are also constants; a loop parameter is a constant within the corresponding loop; a subcomponent or slice of a constant is a constant.

An object that is not a constant is called a variable (in particular, the object declared by an object declaration that does not include the reserved word constant is a variable). The only ways to change the value of a variable are either directly by an assignment, or indirectly when the variable is updated (see 6.2) by a procedure or entry call statement (this action can be performed either on the variable itself, on a subcomponent of the variable, or on another variable that has the given variable as subcomponent).

The elaboration of an object declaration proceeds as follows:

  1. The subtype indication or the constrained array definition is first elaborated. This establishes the subtype of the object.

  2. If the object declaration includes an explicit initialization, the initial value is obtained by evaluating the corresponding expression. Otherwise any implicit initial values for the object or for its subcomponents are evaluated.

  3. The object is created.

  4. Any initial value (whether explicit or implicit) is assigned to the object or to the corresponding subcomponent.

Implicit initial values are defined for objects declared by object declarations, and for components of such objects, in the following cases:

In the case of a component that is itself a composite object and whose value is defined neither by an explicit initialization nor by a default expression, any implicit initial values for components of the composite object are defined by the same rules as for a declared object.

The steps (a) to (d) are performed in the order indicated. For step (b), if the default expression for a discriminant is evaluated, then this evaluation is performed before that of default expressions for subcomponents that depend on discriminants, and also before that of default expressions that include the name of the discriminant. Apart from the previous rule, the evaluation of default expressions is performed in some order that is not defined by the language.

The initialization of an object (the declared object or one of its subcomponents) checks that the initial value belongs to the subtype of the object; for an array object declared by an object declaration, an implicit subtype conversion is first applied as for an assignment statement, unless the object is a constant whose subtype is an unconstrained array type. The exception CONSTRAINT_ERROR is raised if this check fails.

The value of a scalar variable is undefined after elaboration of the corresponding object declaration unless an initial value is assigned to the variable by an initialization (explicitly or implicitly).

If the operand of a type conversion or qualified expression is a variable that has scalar subcomponents with undefined values, then the values of the corresponding subcomponents of the result are undefined. The execution of a program is erroneous if it attempts to evaluate a scalar variable with an undefined value. Similarly, the execution of a program is erroneous if it attempts to apply a predefined operator to a variable that has a scalar subcomponent with an undefined value.

Examples of variable declarations:

    COUNT, SUM  : INTEGER;
    SIZE        : INTEGER range 0 .. 10_ := 0;
    SORTED      : BOOLEAN := FALSE;
    COLOR_TABLE : array(1 .. N) of COLOR;
    OPTION      : BIT_VECTOR(1 .. 10) := (others => TRUE); 

Examples of constant declarations:

    LIMIT     : constant INTEGER := 10_ 
    LOW_LIMIT : constant INTEGER := LIMIT/10;
    TOLERANCE : constant REAL := DISPERSION(1.15);

Note:

The expression initializing a constant object need not be a static expression (see 4.9). In the above examples, LIMIT and LOW_LIMIT are initialized with static expressions, but TOLERANCE is not if DISPERSION is a user-defined function.

References: access type, assignment, assignment compound delimiter, component, composite type, constrained array definition, constrained subtype, constraint_error exception, conversion, declaration, default expression for a discriminant, default initial value for an access type, depend on a discriminant, designate, discriminant, elaboration, entry, evaluation, expression, formal parameter, generic formal parameter, and 12.3, generic unit, in some order, limited type, mode in, package, predefined operator, primary, private type, qualified expression, reserved word, scalar type, slice, subcomponent, subprogram, subtype, subtype indication, task, task type, type, visible part.

3.2.2. Number Declarations

[PREVIOUS][UP]

A number declaration is a special form of constant declaration. The type of the static expression given for the initialization of a number declaration must be either the type universal_integer or the type universal_real. The constant declared by a number declaration is called a named number and has the type of the static expression.

Note:

The rules concerning expressions of a universal type are explained in section 4.10. It is a consequence of these rules that if every primary contained in the expression is of the type universal_integer, then the named number is also of this type. Similarly, if every primary is of the type universal_real, then the named number is also of this type.

Examples of number declarations:

  PI            : constant := 3.14159_26536; -- a real number
  TWO_PI        : constant := 2.0*PI;        -- a real number
  MAX           : constant := 5              -- an integer number
  POWER_16      : constant := 2**16;         -- the integer 65_536
  ONE, UN, EINS : constant := 1;             -- three different names for 1

References: identifier, primary, static expression, type, universal_integer type, universal_real type, universal type, and 4.


[INDEX][CONTENTS]