[Top][All Lists]

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

Re: GOOPS slot access with side-effect

From: Clinton Ebadi
Subject: Re: GOOPS slot access with side-effect
Date: Sat, 01 May 2010 12:39:07 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Panicz Maciej Godek <address@hidden> writes:

> Welcome back :)
> Long time no see
> I have a little question concerning GOOPS.
> (I didn't find it anywhere in the manual)
> Is there any way to force a certain function
> to be invoked during slot access?
> Say, I have a class definition:
> (define-class <A> ()
>   (b #:init-value 0))
> and a certian function f
> (define (f value) (display (string-append "setting b to "
> (number->string value))))
> and now I would like f to be invoked whenever I call
> (slot-set! (make <A>) 'b 5)
> Do I need to redefine slot-set! to consider wherher
> it attempts to access a certain slot of an instance
> of a certain class, or is this option somehow included
> in the design of GOOPS?

There are at least two reasonable ways of going about this:

You could use a #:virtual slot and override #:slot-ref/#:slot-set! [0]
for that to perform the side effect, and then call slot-{ref,set!} on
the actual slot. E.g.

(define-class <A> ()
  (%b #:init-value 0)
  (b #:virtual #:slot-ref (lambda (i) (slot-ref i '%b))
               #:slot-set (lambda (i n) perform side effects etc
                                        (slot-set! i '%b n))))

You could alternatively define an #:accessor or #:writer in the slot
(for documentation) and then override that method. But then you have to
make it part of your protocol that all access goes through that accessor
rather than slot-ref/slot-set! (generally a good idea anyway).

[0] (info "(guile) Slot Options") /

"Karen loved animals. Unfortunately the cheetahs betrayed her trust,"
Libot said.

Attachment: pgpUe7pRzNGkt.pgp
Description: PGP signature

reply via email to

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