This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Indirect object
- To: guile at sourceware dot cygnus dot com
- Subject: Indirect object
- From: Keisuke Nishida <kxn30 at po dot cwru dot edu>
- Date: 05 May 2000 14:03:10 -0400
- References: <m3pur1nm6x.fsf@kei.cwru.edu> <m3n1m5nhtc.fsf@kei.cwru.edu>
I wrote:
> I believe make-dynamic-object can be implemented, but I'm not sure
> whether it is a good thing or not. What do people think?
I realized that I'm not talking about dynamic binding but about indirect
reference to an object. The binding between a symbol and a location
does not change, but the value associated with a location changes
dynamically. So does it make sense to create a new type "indirect
object"? Or is this already possible with Guile?
- Primitive Procedure: make-indirect-object CONSTRUCTOR GETTER SETTER
Create an indirect object and return it.
CONSTRUCTOR is a thunk that creates the initial value of the object.
GETTER is a procedure dynamically called whenever the indirect object
is referred. GETTER takes one argument, which is the return value of
CONSTRUCTOR.
SETTER is a procedure dynamically called whenever the indirect object
is set. SETTER takes two arguments, the return value of CONSTRUCTOR
and the new value to be set.
Example:
;; foo is like a normal variable, except it always returns a different
;; copy of a string.
(define foo (make-indirect-object
(lambda () (make-variable ""))
(lambda (var) (string-copy (variable-ref var)))
(lambda (var val) (variable-set! var val))))
(set! foo "Hello")
(eq? foo foo) => #t
(eqv? foo foo) => #f
(equal? foo foo) => #t
I guess this is naturally integrated with Scheme.
-- Kei