3.17 Assignment: set! and set!-values (original) (raw)

3.17 Assignment: set! and set!-values🔗

+Assignment: set! in The Racket Guide introduces set!.

Otherwise, evaluates expr and installs the result into the location for id, which must be bound as a local variable or defined as a top-level variable or module-level variable. If id refers to an imported binding, a syntax error is reported. If id refers to a top-level variable that has not been defined, the exn:fail:contract exception is raised.

See also compile-allow-set!-undefined.

Examples:

> (define x 12)
> (set! x (add1 x))
> x
13
6
> (set! i-am-not-defined 10)
set!: assignment disallowed;
cannot set variable before its definition
variable: i-am-not-defined
in module: top-level

(set!-values (id ...) expr)

Assuming that all ids refer to variables, this form evaluatesexpr, which must produce as many values as suppliedids. The location of each id is filled with the corresponding value from expr in the same way as forset!.

Example:

More generally, the set!-values form is expanded to

which triggers further expansion if any id has a transformer binding to an assignment transformer.