A location is a place where a value can be stored.
An lvalue is an expression that refers to a location.
(The name "lvalue" refers to the fact that the left operand
of set!
is an lvalue.)
The only kind of lvalue in standard Scheme is a variable.
Kawa also allows computed lvalues. These are procedure
calls used in "lvalue context", such as the left operand of set!
.
You can only use procedures that have an associated setter.
In that case, (set! (f arg ...) value)
is equivalent to ((setter f) arg ... value)
Currently, only a few procedures have associated setter
s,
and only builtin procedures written in Java can have setter
s.
For example:
(set! (car x) 10)
is equivalent to:
((setter car) x 10)
which is equivalent to:
(set-car! x 10)
Gets the "setter procedure" associated with a "getter procedure". Equivalent to
(procedure-property
. By convention, a setter procedure takes the same parameters as the "getter" procedure, plus an extra parameter that is the new value to be stored in the location specified by the parameters. The expectation is that followingprocedure
'setter)((setter
then the value ofproc
)args
...value
)(
will beproc
args
...)value
.The
setter
ofsetter
can be used to set thesetter
property. For example the Scheme prologue effectively does the following:(set! (setter vector-set) vector-set!)
Kawa also gives you access to locations as first-class values:
Returns a location object for the given
lvalue
. You can get its value (by applying it, as if it were a procedure), and you can set its value (by usingset!
on the application). Thelvalue
can be a local or global variable, or a procedure call using a procedure that has asetter
.(define x 100) (define lx (location x)) (set! (lx) (cons 1 2)) ;; set x to (1 . 2) (lx) ;; returns (1 . 2) (define lc (location (car x))) (set! (lc) (+ 10 (lc))) ;; x is now (11 . 2)
Syntax: define-alias
variable
lvalue
Define
variable
as an alias forlvalue
. In other words, makes it so that(location
is equivalent tovariable
)(location
. This works both top-level and inside a function.lvalue
)
Some people might find it helpful to think of a location
as a settable thunk. Others may find it useful to
think of the location
syntax as similar to the C ‘&
’ operator;
for the ‘*
’ indirection operator, Kawa uses procedure application.