Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


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

Subsections



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 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 [*].)

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 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 := 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. Whereas, 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.)


Cascaded Assignments

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

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:= expr2
where op is any of the following:
    + - * / % ^ | & || &&
The assignment is identical to:
expr1 := expr1 op expr2
except expr1 is evaluated only 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 like ordinary assignments. In the following assignment:

    a *:= b +:= 4
first 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 [*].)


next up previous contents index
Next: Indexing Up: Expressions Previous: Logical Expressions   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15