Locations

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 setters, and only builtin procedures written in Java can have setters.

For example:

(set! (car x) 10)

is equivalent to:

((setter car) x 10)

which is equivalent to:

(set-car! x 10)

Procedure: setter procedure

Gets the "setter procedure" associated with a "getter procedure". Equivalent to (procedure-property procedure 'setter). 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 following ((setter proc) args ... value) then the value of (proc args ...) will be value.

The setter of setter can be used to set the setter 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:

Syntax: location lvalue

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 using set! on the application). The lvalue can be a local or global variable, or a procedure call using a procedure that has a setter.

(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 for lvalue. In other words, makes it so that (location variable) is equivalent to (location lvalue). This works both top-level and inside a function.

Syntax: define-private-alias variable lvalue

Same as define-alias, but the variable is local to the current module.

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.

You can use define-alias to define a shorter type synonym, similar to Java’s import TypeName (single-type-import) declaration:

(define-alias StrBuf java.lang.StringBuffer)