The assignment Statement

Description


•

The assignment operator := assigns to the lhs the value of rhs. The lefthand side of the assignment operator 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. 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.



Examples


>

$f\left(1\right)\u22540$

${f}{}\left({1}\right){:=}{0}$
 (5) 
>

$a\,b\u22542\,3$

${a}{\,}{b}{:=}{2}{\,}{3}$
 (7) 
>

$a\,b\u2254b\,a$

${a}{\,}{b}{:=}{3}{\,}{2}$
 (8) 
>

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

${g}{:=}{\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\u2254g\left(10\right)$

${s}{\,}{t}{:=}{9}{\,}{11}$
 (10) 
>

$j\,{c}_{j}\,f\left(j\right)\u22541\,2\,3$

${j}{\,}{{c}}_{{j}}{\,}{f}{}\left({j}\right){:=}{1}{\,}{2}{\,}{3}$
 (11) 
>

$\mathrm{print}\left(c\right)$

${\mathrm{table}}\left(\left[{1}{\=}{2}\right]\right)$
 (12) 
Examples of typed assignments with assertion failures.
>

$\mathrm{kernelopts}\left(\mathrm{assertlevel}\=2\right)\:$

>

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

>

$b::\mathrm{float}\u2254\left[1\,2\,3\,4\,5\right]$







Download Help Document
Was this information helpful?