
Description


•

The assignment statement lhs := rhs assigns to the lhs the value of rhs. The lefthand side of the assignment must be a name, indexedname, function call, or expression sequence of these.

•

The assignment statement functions as follows.

1.

The lefthand side is resolved to a name. (For more information, see the evaln function).

2.

The righthand side is evaluated as an expression.

3.

The assignment is performed. The value of the assignment statement is the righthand side.

•

If the lefthand side is a sequence of names, the righthand side must evaluate to a sequence of expressions with the same number of components as the lefthand side. This is called a multiple assignment. The sequence of assignments are processed in the following order. First, all of the righthand side expressions are evaluated. Then in a left to right pairwise manner the names on the lefthand side are evaluated and the matching value on the righthand side is assigned to it.

•

Local variables can be declared with type assertions. The syntax is the same as that used for parameters. Whenever an assignment is made to a local variables with such a type assertion, the type of the righthand side is checked after evaluation, but before the assignment is done. If the type of the righthand side does not match, an assertion failure exception is raised.


Similarly, the lefthand side of any assignment can contain a type assertion that is checked before the assignment is carried out.

•

The setting of kernelopts(assertlevel) controls whether these type assertions are checked. Settings are described as follows.


0  Turns off all assertion checking.


1  Checks only assertions made using the ASSERT function.


2  Checks ASSERT assertions, and assignmenttype assertions.



Assignments as Expressions


•

An assignment statement can also be used as an expression, or embedded within most expressions, by enclosing it in parentheses. The result of an assignment expression is the righthand side.

•

The assignment takes place only if and when the part of the expression containing it is evaluated. Except in the case of logical operators, which follow lefttoright McCarthy (shortcircuit) evaluation rules, it is generally not possible to know the order in which subexpressions will be evaluated.



Operator Assignments


•

In addition to assigning an already computed value to a variable, it is possible to combine an operation and assignment into one using an assignment operator. These are:

+=

++

=



=

.=

/=

mod=

^=

,=

=


and=

or=

xor=

implies=

union=

intersect=

minus=




•

Each of these performs the same operation that it would if the = were omitted, using the evaluated left and right hand sides of the assignment operator as its operands. The result is then assigned to the left hand side name.

•

The benefit of using an assignment operator is that the left hand side need only be resolved once. That is, the process of evaluating it to a name (which must be done before evaluating it to a value) is only done once. Thus, in an operator assignment such as A[veryBigExpression] += 1, the index veryBigExpression is only evaluated once. If the assignment were written as A[veryBigExpression] := A[veryBigExpression] + 1, then the index would be evaluated twice.

•

Like simple assignments (:=), an operator assignment can be used within an expression by enclosing it in parentheses. The result of such an embedded assignment is the computed value that was assigned to the left hand side.

•

The increment (+=) and decrement (=) assignment operators each have an even shorter form that can be used when the right hand side is 1. The expression ++x is equivalent to x += 1, and x is equivalent to x = 1.


Unlike the longer forms, the short forms are expressions in their own right, and thus can be used within a larger expression without requiring extra parentheses (except where needed for disambiguation). The value of such an expression is the incremented or decremented value.


The short forms can also be written in postfix form (x++ and x), in which case the effect on their argument, x, is the same, but the value of the expression is the original value of x.

•

If an operator assignment involving the same left hand side appears more than once in an expression, the order in which the assignments are carried out is undefined.


Special Semantics of the ,= Assignment Operator


•

In most cases, the operator assignment a ,= b is equivalent to a = a, b, forming an expression sequence from the values of a and b, and assigning it back to variable a.

•

In the special case where the value of the left hand side is a one dimensional Array or a Vector, it is expanded and the right hand side appended to it inplace. If the right hand side is an expression sequence (e.g., a ,= b,c,d) each element of the sequence is appended separately.


If the left hand side is a one dimensional Array or a Vector with a hardware datatype, and an element of the right hand side is a one dimensional Array or a Vector, then the contents of that Array or Vector are appended to the left hand side.


If the left hand side is a one dimensional Array or a Vector with a datatype of integer[1] (i.e., an array of bytes), and an element of the right hand side is a string, the byte values of the characters in the string are individually appended to the left hand side. Such an Array or Vector can be converted back to a string using the String constructor.


If the left hand side is an object that implements a `,=` method, then that method is invoked to perform the operation. For example, the MutableSet object implements this method such that ms ,= x is equivalent to insert(ms,x) or ms union= MutableSet(x).


In cases other than those above, a right hand side is appended in its entirety as an element of the left hand side.

•

If any of the assignments described above are not possible (e.g. the right hand side is a symbol and the array has datatype float[8]), an exception is raised.




Examples


${{a}}_{{i}}{\u2254}{2}$
 (3) 
${f}{}\left({1}\right){\u2254}{0}$
 (5) 
${a}{,}{b}{\u2254}{2}{,}{3}$
 (7) 
${a}{,}{b}{\u2254}{3}{,}{2}$
 (8) 
>

g := proc(x) (x1,x+1) end proc;

${g}{\u2254}{\mathbf{proc}}\left({x}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{x}{}{1}{\,}{x}{\+}{1}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (9) 
${s}{,}{t}{\u2254}{9}{,}{11}$
 (10) 
>

(j,c[j],f(j)) := 1, 2, 3;

${j}{,}{{c}}_{{j}}{,}{f}{}\left({j}\right){\u2254}{1}{,}{2}{,}{3}$
 (11) 
${table}{}\left(\left[{1}{=}{2}\right]\right)$
 (12) 
Examples of typed assignments with assertion failures.
>

kernelopts(assertlevel=2):

>

F := proc(x) local a::integer; a := x end proc:

>

b::float := [1,2,3,4,5];

Examples of assignments as, or within, expressions.
In the examples below, if a < b then the second assignment never takes place because the second operand of the or is not evaluated when the first is true.
${a}{,}{b}{\u2254}{3}{,}{4}$
 (16) 
>

(smallest := a) <= b or (smallest := b) < a;

${a}{,}{b}{\u2254}{3}{,}{2}$
 (19) 
>

(smallest := a) <= b or (smallest := b) < a;

Examples of operator assignments.
$\left[{5}{\,}{4}\right]$
 (28) 


Compatibility


•

Assignments as expressions and operator assignments are currently not supported in 2D math input in the Standard Interface. They are supported in 1D math input in the Standard Interface, as well as in all forms of input and output in the Commandline user interface.



