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:
• a variable name;
• an element or group of elements of a vector or array (see § 3.1.4, page , § 3.6.3, page , and § 3.7.2, page );
• a field or group of fields of a record (see § 3.4.2, page , and § 3.6.4, page );
• the val operator followed by an lvalue (§ 3.8, page );
• or the const operator followed by an lvalue (§ 3.9, page ).

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 .

## 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 , 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 := 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:

expr op:= expr
where op is any of:
`    + - * / % ^ | & || &&`
The assignment is identical to:
expr := expr op expr
except perhaps expr 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 ):

`    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: Indexing Up: Expressions Previous: Logical Expressions

Thu Nov 13 16:44:05 EST 1997