www.delorie.com/gnu/docs/kawa/kawa_41.html   search  
 
Buy GNU books!


Kawa, the Java-based Scheme system

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.13 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) value arg ...) (It is possible the definition will change to ((setter f) arg ... value) if Guile goes for that.) 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:
 
(set-car! x 10)

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.

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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003