Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
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 left-hand-side is 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. (See § 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 it is either then the underlying value of the resulting reference is modified. If it is not either of these, 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. Whereas, 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.14, 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.
As in C, assignment expressions can include an operator immediately before the := token to indicate compound assignment. The general form of a compound assignment is:
expr1 op:= expr2where op is any of the following:
+ - * / % ^ | & || &&The assignment is identical to:
expr1 := expr1 op expr2except expr1 is evaluated only 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 like ordinary assignments. In the following assignment:
a *:= b +:= 4first increments b by 4, and then multiplies a by the new value of b, storing the result back into a. (See § 4.6.3, page .)