Skip to content

assignment statement

Cameron Purdy edited this page Jun 28, 2021 · 2 revisions

An AssignmentStatement is used to assign a value to a variable, a property, or an element of an indexed data structure. The AssignmentStatement has significant overlap with the VariableStatement, which declares a new local variable and optionally assigns it a value, except that the AssignmentStatement does not declare a variable, and it supports other forms of L-Values (left-hand side values, i.e. something that can be assigned to) in addition to local variables.

The simplest example is a local variable (or property name) being assigned a constant value:

first = False;
color = Red;
size  = 5;

Properties and arrays can also be assigned to:

point.x  = 0;
names[2] = "CrazyBob";

In addition to the straight-through assignment operator, there are a number of specialized assignment operators:

operator description
= straight-through assignment
*= multiply-assign
/= divide-assign
%= modulo-assign
+= add-assign
-= subtract-assign
<<= shift-left-assign
>>= shift-right-assign
>>>= unsigned-shift-right-assign
&= and-assign
&&= and-assign (logical, short-circuiting)
|= or-assign
||= or-assign (logical, short-circuiting)
^= xor-assign
?:= elvis-assign
:= conditional assign
?= not-null assign

Most of these assignment operators are well-known in the field of programming. Given any operator, such as the multiplication operator *, combined with the assignment character, in this case *=, the statement A *= B; is defined as A = A * B;. The exceptions to this are the short-circuiting logical-assignment operators, the elvis-assign, the conditional assign and the not-null assign. Here are the explanations:

example long form equivalent
A &&= B; if (A) { A = B; }
A ||= B; if (!A) { A = B; }
A ?= B A = (B)? or if (B != Null) { A = B; }
A ?:= B if (A == Null) { A = B; }
A := B if (val temp := B) { A = temp; }

Both the ?= and := operators can be used as part of a Condition, such as appears in an if or while statement. Assume two methods:

String? couldBeNull();
conditional String couldBeFalse();

if (String s ?= couldBeNull())
    {
    // the assignment only occurs, and the Condition is only
    // True, if couldBeNull() returned a String, and not Null
    }

if (String s := couldBeFalse())
    {
    // the assignment only occurs, and the Condition is only
    // True, if couldBeFalse() returned True and a String
    }

The R-Value must be assignment-compatible with the L-Value.

For straight-through assignment, the Assignee is reachable if the AssignmentStatement is reachable, and the determination of the Assignee L-Value completes if the Asignee is reachable and the calculation of any target object reference and/or array indexes completes. The R-Value Expression is reachable if the L-Value calculation completes. The AssignmentStatement completes if the L-Value calculation can short-circuit, or if the L-Value calculation can complete and either the R-Value can short-circuit or can complete.

Definite assignment rules:

  • The VAS before the Assignee L-Value calculation is the VAS before the AssignmentStatement.
  • The VAS before the Expression R-Value calculation is the VAS after the L-Value calculation.
  • The VAS after the AssignmentStatement is the VAS after the R-Value calculation plus the assignment of the L-Value itself, joined with the VAS from any point in either the Assignee L-Value calculation and Expression R-Value calculation that can short-circuit.
    AssignmentStatement:
        Assignee AssignmentOperator Expression ;

    Assignee:
        Assignable
        ( AssignableList , Assignable )

    AssignableList:
        Assignable
        AssignableList , Assignable
    
    Assignable:
        Name
        TernaryExpression . Name
        TernaryExpression ArrayIndexes
    
    AssignmentOperator:
        =
        *=
        /=
        %=
        +=
        -=
        <<=
        >>=
        >>>=
        &=
        &&=
        |=
        ||=
        ^=
        ?:=
        :=
        ?=