An assignment expression assigns a value to a variable and also yields that value as the overall value of the expression.
An assignment expression has the form:
expression := expressionThe left-hand-side must be an lvalue; that is, something that can be assigned to:
If the left-hand-side is a variable name or a record field then the right-hand-side can be any valid Glish expression. If it's a vector element or group of elements then the right-hand-side must have a compatible type, and if the right-hand-side's type is higher then the vector is converted to that type (see § 3.1.4, page ).
If the left-hand-side is a group of record fields then the right-hand-side must be a record, and the assignment is done field-by-field, left-to-right, as explained in § 3.6.4, page .
If the left-hand-side is a val expression then its lvalue is inspected to see whether its value is either a reference or the target of reference. If so then the underlying value of the resulting reference is modified. If not then the assignment is done as though val was not present. For example,
a := 5 val a := 9is equivalent to
a := 5 a := 9and after executing
a := 5 b := ref a val a := 9both a and b are 9, while after executing
a := 5 b := ref a a := 9a is 9 but b remains 5 (and the link between a and b is severed). See § 3.8, page , for details.
Because assignment expressions yield the assigned expression as their value, and because assignment is right-associative (see § 4.13, page ), assignments can be naturally ``cascaded'':
a := b := 5first assigns 5 to b and then also to a. More complicated expressions are possible, too:
a := (b := 5) * 4assigns 5 to b and 20 to a.
Like in C, assignment expressions can include an operator immediately before the := token to indicate compound assignment. The general form of a compound assignment is:
expr op:= exprwhere op is any of:
+ - * / % ^ | & || &&The assignment is identical to:
expr := expr op exprexcept perhaps expr is only evaluated once (not presently guaranteed by the language).
Thus, for example:
x +:= 5adds 5 to x, identically to:
x := x + 5
You can cascade compound assignments just like ordinary assignments (§ 4.6.3, page ):
a *:= b +:= 4first increments b by 4, and then multiplies a by the new value of b, storing the result back into a.