next up previous contents index
Next: Indexing Up: Expressions Previous: Logical Expressions

Assignment Expressions


  An assignment expression assigns a value to a variable and also yields that value as the overall value of the expression.


Assignment Syntax

  An assignment expression has the form:

expression := expression
  The 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 gif).

    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 gif.

Assigning reference Values

  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 := 9
is equivalent to
    a := 5
    a := 9
and after executing
    a := 5
    b := ref a
    val a := 9
both a and b are 9, while after executing
    a := 5
    b := ref a
    a := 9
a is 9 but b remains 5 (and the link between a and b is severed). See § 3.8, page gif, for details.  

Cascaded Assignments


  Because assignment expressions yield the assigned expression as their value, and because assignment is right-associative (see § 4.13, page gif), assignments can be naturally ``cascaded'':

    a := b := 5
first assigns 5 to b and then also to a. More complicated expressions are possible, too:
    a := (b := 5) * 4
assigns 5 to b and 20 to a.  

Compound Assignment


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:

exprtex2html_wrap_inline15496 op:= exprtex2html_wrap_inline15498
where op is any of:
    + - * / % ^ | & || &&
The assignment is identical to:
exprtex2html_wrap_inline15496 := exprtex2html_wrap_inline15496 op exprtex2html_wrap_inline15498
except perhaps exprtex2html_wrap_inline15496 is only evaluated once (not presently guaranteed by the language).

Thus, for example:

    x +:= 5
adds 5 to x, identically to:
    x := x + 5

You can cascade compound assignments just like ordinary assignments (§ 4.6.3, page gif):

    a *:= b +:= 4
first increments b by 4, and then multiplies a by the new value of b, storing the result back into a.


next up previous contents index
Next: Indexing Up: Expressions Previous: Logical Expressions

Thu Nov 13 16:44:05 EST 1997