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


5.2 Assignment Statements

  1. An assignment_statement replaces the current value of a variable with the result of evaluating an expression.

    Syntax

  2. assignment_statement ::= variable_name := expression;
    
  3. The execution of an assignment_statement includes the evaluation of the expression and the assignment of the value of the expression into the target. An assignment operation (as opposed to an assignment_statement) is performed in other contexts as well, including object initialization and by-copy parameter passing. The target of an assignment operation is the view of the object to which a value is being assigned; the target of an assignment_statement is the variable denoted by the variable_name.

    Name Resolution Rules

  4. The variable_name of an assignment_statement is expected to be of any nonlimited type. The expected type for the expression is the type of the target.

    Legality Rules

  5. The target denoted by the variable_name shall be a variable.
  6. If the target is of a tagged class-wide type T'Class, then the expression shall either be dynamically tagged, or of type T and tag-indeterminate, See section 3.9.2 Dispatching Operations of Tagged Types.

    Dynamic Semantics

  7. For the execution of an assignment_statement, the variable_name and the expression are first evaluated in an arbitrary order.
  8. When the type of the target is class-wide:
    1. If the expression is tag-indeterminate, See section 3.9.2 Dispatching Operations of Tagged Types, then the controlling tag value for the expression is the tag of the target;
    2. Otherwise (the expression is dynamically tagged), a check is made that the tag of the value of the expression is the same as that of the target; if this check fails, Constraint_Error is raised.

  1. The value of the expression is converted to the subtype of the target. The conversion might raise an exception, See section 4.6 Type Conversions.
  2. In cases involving controlled types, the target is finalized, and an anonymous object might be used as an intermediate in the assignment, as described in See section 7.6.1 Completion and Finalization. In any case, the converted value of the expression is then assigned to the target, which consists of the following two steps:
    1. The value of the target becomes the converted value.
    2. If any part of the target is controlled, its value is adjusted as explained in clause See section 7.6 User-Defined Assignment and Finalization.

    NOTES

  1. (2) The tag of an object never changes; in particular, an assignment_statement does not change the tag of the target.
  2. (3) The values of the discriminants of an object designated by an access value cannot be changed (not even by assigning a complete value to the object itself) since such objects are always constrained; however, subcomponents of such objects may be unconstrained.

    Examples

  3. Examples of assignment statements:
  4. Value := Max_Value - 1;
    Shade := Blue;
    
  5. Next_Frame(F)(M, N) := 2.5;  --  See section 4.1.1 Indexed Components
    U := Dot_Product(V, W);      --  See section 6.3 Subprogram Bodies
    
  6. Writer := (Status => Open, Unit => Printer, Line_Count => 60);
    -- See section 3.8.1 Variant Parts and Discrete Choices
    
    Next_Car.all := (72074, null);
    --  See section 3.10.1 Incomplete Type Declarations
    
  7. Examples involving scalar subtype conversions:
  8. I, J : Integer range 1 .. 10 := 5;
    K    : Integer range 1 .. 20 := 15;
     ...
    
  9. I := J;  --  identical ranges
    K := J;  --  compatible ranges
    J := K;  --  will raise Constraint_Error if K > 10
    
  10. Examples involving array subtype conversions:
  11. A : String(1 .. 31);
    B : String(3 .. 33);
     ...
    
  12. A := B;  --  same number of components
    
  13. A(1 .. 9)  := "tar sauce";
    A(4 .. 12) := A(1 .. 9);  --  A(1 .. 12) = "tartar sauce"
    

    NOTES

  14. (4) Notes on the examples: Assignment_statements are allowed even in the case of overlapping slices of the same array, because the variable_name and expression are both evaluated before copying the value into the variable. In the above example, an implementation yielding A(1 .. 12) = "tartartartar" would be incorrect.


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