unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* GOOPS slot access with side-effect
@ 2010-05-01 11:12 Panicz Maciej Godek
  2010-05-01 16:39 ` Clinton Ebadi
  0 siblings, 1 reply; 2+ messages in thread
From: Panicz Maciej Godek @ 2010-05-01 11:12 UTC (permalink / raw)
  To: guile-user

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?

Best regards
MG




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: GOOPS slot access with side-effect
  2010-05-01 11:12 GOOPS slot access with side-effect Panicz Maciej Godek
@ 2010-05-01 16:39 ` Clinton Ebadi
  0 siblings, 0 replies; 2+ messages in thread
From: Clinton Ebadi @ 2010-05-01 16:39 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1726 bytes --]

Panicz Maciej Godek <godek.maciek@gmail.com> 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") / 
    http://www.gnu.org/software/guile/docs/master/guile.html/Slot-Options.html

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

[-- Attachment #2: Type: application/pgp-signature, Size: 229 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-05-01 16:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-01 11:12 GOOPS slot access with side-effect Panicz Maciej Godek
2010-05-01 16:39 ` Clinton Ebadi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).