This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: MOP for slots?
- To: ole dot rohne at cern dot ch
- Subject: Re: MOP for slots?
- From: Neil Jerram <neil at ossau dot uklinux dot net>
- Date: Mon, 17 Apr 2000 18:58:09 +0100
- CC: guile at sourceware dot cygnus dot com
- References: <14583.4220.651256.906236@pcedu1.cern.ch>
Ole Myren Rohne writes:
My current understanding is that there is no such thing in goops -
there are no slot metaobjects? I'm not asking out of interest in slot
metaobjects as such, rather because of two loose ideas that I have in
mind:
[...]
As an absolute beginner with MOPs, I'd apprechiate any comments on why
my ideas are bad, how they can easily be implemented without slot
metaobjects, or maybe why slot metaobjects would be nice to have...
I think you are right that there aren't currently slot metaobjects in
GOOPS (although I think Mikael plans to add them), but you can get a
bit of leverage over the behaviour of slots by using the #:allocation
slot option.
Here's some doc that I wrote for the GOOPS reference manual. But note
that it's not really tested, so may not be 100% correct.
Slot allocation options are processed when defining a new class by
the generic function `compute-get-n-set', which is specialized by
the class's metaclass. Hence new types of slot allocation can be
implemented by defining a new metaclass and a method for
`compute-get-n-set' that is specialized for the new metaclass.
Suppose you wanted to create a large number of instances of some
class with a slot that should be shared between some but not all
instances of that class - say every 10 instances should share the
same slot storage. The following example shows how to implement
and use a new type of slot allocation to do this.
(define-class <batched-allocation-metaclass> (<class>))
(let ((batch-allocation-count 0)
(batch-get-n-set #f))
(define-method compute-get-n-set ((class <batched-allocation-metaclass>) s)
(case (slot-definition-allocation s)
((#:batched)
;; If we've already used the same slot storage for 10 instances,
;; reset variables.
(if (= batch-allocation-count 10)
(begin
(set! batch-allocation-count 0)
(set! batch-get-n-set #f)))
;; If we don't have a current pair of get and set closures,
;; create one. make-closure-variable returns a pair of closures
;; around a single Scheme variable - see goops.scm for details.
(or batch-get-n-set
(set! batch-get-n-set (make-closure-variable)))
;; Increment the batch allocation count.
(set! batch-allocation-count (+ batch-allocation-count 1))
batch-get-n-set)
;; Call next-method to handle standard allocation types.
(else (next-method)))))
(define-class <class-using-batched-slot> ()
...
(c #:allocation #:batched)
...
#:metaclass <batched-allocation-metaclass>)
I hope this helps a little.
Neil