guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: lazy evaluation: Goops class/instance customization


From: Mikael Djurfeldt
Subject: Re: lazy evaluation: Goops class/instance customization
Date: Tue, 20 May 2003 19:53:33 +0200
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (gnu/linux)

Maarten Grachten <address@hidden> writes:

> Thus, I came up with the following code:
>
> (define-class <Lazy-Eval-Metaclass> (<class>))
>
> (define (make-lazy-closure-variable class)
>   (let ((shared-variable (make-unbound)))
>     (list (lambda (o) (primitive-eval (force shared-variable)))
>           (lambda (o v) (set! shared-variable v)))))
>
> (define-method (compute-get-n-set (c <Lazy-Eval-Metaclass>) slot-defs)
>       (make-lazy-closure-variable c))
>
> (defmacro make-lazy (specification . body)
>       (let ((attributes (map (lambda (x)
>                                 (if (keyword? x) x (delay x)))
>                                body)))
>               `(make ,specification
>                        ,@attributes)))
>
> (define-class <lazy-class> ()
>       (s1 #:init-keyword #:s1 #:accessor s1)
>       #:metaclass <Lazy-Eval-Metaclass>)

[...]

> Does anybody see how my code above can be easily changed so as to
> admit the lazy evaluation?

Please try to replace your version of compute-get-n-set with this:
(An advantage of this code is that it will work for all kinds of
slots, including instance slots and class slots.)

(define-method (compute-get-n-set (class <Lazy-Eval-Metaclass>) slot)
  (let* ((g-n-s (next-method))          ;call "system" compute-get-n-set
         ;; now some "black magic"
         (getter (if (pair? g-n-s) (car g-n-s) (standard-get g-n-s)))
         (setter (if (pair? g-n-s) (cadr g-n-s) (standard-set g-n-s))))
    (list (lambda (o)
            (force (getter o)))
          setter)))

However, `standard-get' and `standard-set' are not exported by the
standard goops module interface, so you have to use the module
(oop goops internal) instead of (oop goops).

When trying to come up with an elegant solution to your problem (which
the above isn't) I realized that we should probably extend the
metaobject prototcol to allow for what you want to do more cleanly and
conveniently.  I'll put that on my TODO list.

Mikael D.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]