5.2 Assignment Statements
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[An assignment_statement
replaces the current value of a variable with the result of evaluating an expression
.]
Syntax
2assignment_statement
::=
variable_name
:= expression
;
3The 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
.
assignment_statement
. The assignment operation is just one part of the execution of an assignment_statement
. The assignment operation is also a part of the execution of various other constructs; see 7.6.1, “Completion and Finalization” for a complete list. Note that when we say, “such-and-such is assigned to so-and-so”, we mean that the assignment operation is being applied, and that so-and-so is the target of the assignment operation. Name Resolution Rules
4/2The variable_name
of an assignment_statement
is expected to be of any type. The expected type for the expression
is the type of the target.
assignment_statement
as a whole is a “complete context”, so if the variable_name
of an assignment_statement
is overloaded, the expression
can be used to help disambiguate it. For example: type P1 is access R1;
type P2 is access R2;
4.cfunction F return P1;
function F return P2;
4.dX : R1;
begin
F.all := X; -- Right hand side helps resolve left hand side
Legality Rules
5/2The target [denoted by the variable_name
] shall be a variable of a nonlimited type.
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 3.9.2).
assignment_statement
), a statically tagged initialization expression is permitted, since there is no chance for confusion (or Tag_Check failure). Also, in an object initialization, tag-indeterminate expressions of any type covered by T'Class would be allowed, but with an assignment_statement
, that might not work if the tag of the target was for a type that didn't have one of the dispatching operations in the tag-indeterminate expression. Dynamic Semantics
7For the execution of an assignment_statement
, the variable_name
and the expression
are first evaluated in an arbitrary order.
expression
, but that does not necessarily require evaluation of the variable_name
, as pointed out by the ACID. When the type of the target is class-wide:
- If the
expression
is tag-indeterminate (see 3.9.2), then the controlling tag value for theexpression
is the tag of the target;
- Otherwise [(the
expression
is dynamically tagged)], a check is made that the tag of the value of theexpression
is the same as that of the target; if this check fails, Constraint_Error is raised.
The value of the expression
is converted to the subtype of the target. [The conversion can raise an exception (see 4.6).]
In cases involving controlled types, the target is finalized, and an anonymous object can be used as an intermediate in the assignment, as described in 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:
assignment_statement
does nothing. - The value of the target becomes the converted value.
- If any part of the target is controlled, its value is adjusted as explained in subclause 7.6.
assignment_statement
. assignment_statement
does not change the tag of the target.assignment_statement
s is performed only for the value of the right-hand side expression as a whole; it is not performed for subcomponents of the value.assignment_statement
may require consideration of the expression if the variable name can be interpreted as the name of a variable designated by the access value returned by a function call, and similarly, as a component or slice of such a variable (see 8.6, “The Context of Overload Resolution”). Examples
17Examples of assignment statements:
Value := Max_Value - 1;
Shade := Blue;
19Next_Frame(F)(M, N) := 2.5; -- see 4.1.1
U := Dot_Product(V, W); -- see 6.3
20/4Writer := (Status => Open, Unit => Printer, Line_Count => 60); -- see 3.8.1
Next.all := (72074, null, Head); -- see 3.10.1
21Examples involving scalar subtype conversions:
I, J : Integer range 1 .. 10 := 5;
K : Integer range 1 .. 20 := 15;
...
23I := J; -- identical ranges
K := J; -- compatible ranges
J := K; -- will raise Constraint_Error if K > 10
24Examples involving array subtype conversions:
A : String(1 .. 31);
B : String(3 .. 33);
...
26A := B; -- same number of components
27A(1 .. 9) := "tar sauce";
A(4 .. 12) := A(1 .. 9); -- A(1 .. 12) = "tartar sauce"
Assignment_statement
s 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. Extensions to Ada 83
assignment_statement
s (see 7.6, “Assignment and Finalization”). Wording Changes from Ada 83
assignment_statement
is a subcomponent that depends on discriminants. Incompatibilities With Ada 95
type AccNonLim is access NonLim;
function Foo (Arg : in Integer) return AccNonLim;
type AccLim is access Lim;
function Foo (Arg : in Integer) return AccLim;
Foo(2).all := Foo(1).all;
5.2.1 Target Name Symbols
1/5@, known as the target name of an assignment statement, provides an abbreviation to avoid repetition of potentially long names in assignment statements.
Syntax
2/5target_name
::=
@
Name Resolution Rules
3/5[If a target_name
occurs in an assignment_statement
A, the variable_name
V of A is a complete context. The target name is a constant view of V, having the nominal subtype of V.]
Legality Rules
4/5A target_name
shall appear only in the expression
of an assignment_statement
.
Dynamic Semantics
5/5For the execution of an assignment_statement
with one or more target_name
s appearing in its expression
, the variable_name
V of the assignment_statement
is evaluated first to determine the object denoted by V, and then the expression
of the assignment_statement
is evaluated with the evaluation of each target_name
yielding a constant view of the the target whose properties are otherwise identical to those of the view provided by V. The remainder of the execution of the assignment_statement
is as given in subclause 5.2.
target_name
is aliased and the nominal subtype of the target_name
. It was too weird to give separate rules for static and dynamic properties that said almost the same thing. target_name
can be erroneous if the variable_name
V is a discriminant-dependent component, and some other constituent of the expression
modifies the discriminant governing the component V. The assignment probably would be erroneous anyway, but the use of a target_name
eliminates the possibility that a later evaluation of V raises an exception before any erroneous execution occurs. See 3.7.2. Examples
6/5Examples of the use of target name symbols:
Board(1, 1) := @ + 1.0; -- An abbreviation for Board(1, 1) := Board(1, 1) + 1.0;
-- (Board is declared in 3.6.1).
8/5My_Complex_Array : array (1 .. Max) of Complex; -- See 3.3.2, 3.8.
...
-- Square the element in the Count (see 3.3.1) position:
My_Complex_Array (Count) := (Re => @.Re**2 - @.Im**2,
Im => 2.0 * @.Re * @.Im);
-- A target_name can be used multiple times and as a prefix if desired.