This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Indirect object


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]