Previous section: Binding Multiple Values

Dylan reference manual -- Assignment

Assignment

The Dylan special form := is used to set variables to new values. It can also be used as an alternate syntax for calling setter functions and macros.
  place  := new-value   =>  new-value	[Special Form]
:= stores new-value in place. Subsequent evaluations of place will yield new-value. := returns new-value.

place commonly has the syntax of a variable name. Dylan also allows extended formats (described below). If place is not a variable name or one of these extended formats, an error is signaled.

If place is a variable name, then new-value is stored in the corresponding variable (which may be a lexical or module variable). An error is signaled if there is no lexical or module variable corresponding to the place , if the corresponding variable is a read-only variable, or if the corresponding variable is specialized to some type and the new-value is not an instance of that type.

? define variable foo = 10;
10
? foo             // this is a variable
10                // this is the variable's contents
? foo := 10 + 10;
20
? foo
20

Extended form

If place is not a variable name, then it may have the syntax of a call to a function. This is called the extended form of :=. In this case, the function call indicates an accessible location. The call to := should change what future accesses of the location will return.
? define variable foo = vector (10, 6, 8, 5);
foo
? element(foo, 2)
8
? element(foo, 2) := "bar"
"bar"
? foo
#[10, 6, "bar", 5]
In the general case,
name ( arg1, ... argn ) := new-value
means roughly
name-setter ( new-value, arg1, ... argn )
In functional := expressions

function-name(arg1, ..., argn) := value

where function-name is not a macro name, arg1 ... argn are evaluated first in that order, followed by new-value.

The evaluation time of the variable function-name-setter, which this expression is defined to invoke, is unspecified.

Extended form using array reference or slot reference syntax

The special syntactic shorthands for array reference and slot reference are also allowed as part of the extended form of :=.

For example, the two forms

foo[2] := "quux"
element (foo, 2) := "quux"
are equivalent to
element-setter ("quux", foo, 2).
In . := expressions

argument.function-name := value

where function-name is not a macro name, argument is evaluated first, followed by value.

The evaluation time of the variable function-name-setter, which this expression is defined to invoke, is unspecified.

In implicit element := expressions

sequence[arg1, ..., argn] := value

sequence is evaluated first, then arg1 ... argn in that order, then value. The evaluation time of the variable element-setter, which this expression is defined to invoke, is unspecified.

Next section: True and False