value cell for virtual slots (Re: stklos question)

From: Shiro Kawai <>
Date: Tue, 13 Jan 1998 11:43:21 -1000

> From: Erick Gallesio <>
> Date: Tue, 13 Jan 1998 10:07:13 +0100
> For instance:
> (define setter! #f)
> (define getter #f)
> (let ((var 0))
> (set! getter (lambda (o) var))
> (set! setter! (lambda (o v) (set! var v))))
> (define-class <foo> ()
> ((slot :allocation :virtual :slot-set! setter! :slot-ref getter)))

I think by this method all the instances of class <foo> shares the
same virtual cell (closed variable "var"), so it will behave like
:allocation :class. Also it won't work with :initform option
(see the Erick's comment in stklos.stk).

When I want to have per-instance slots with custom slot-set! and
slot-ref, usually I create a vector for each instance to hold
"hidden" values. (Still the vector itself is not hidden.
I don't know how to hide it completely)

(define-class <foo-meta> (<class>)
  ((hidden-slot-count :initform 0)))

(define-method compute-get-n-set ((class <foo-meta>) s)
  (if (eqv? (get-slot-allocation s) :hidden)
      (let* ((index (slot-ref class 'hidden-slot-count))
             ;; you can add any features to following getter and setter
             (getter (lambda (o) (vector-ref (slot-ref o 'values) index)))
             (setter (lambda (o v) (vector-set! (slot-ref o 'values) index v))))
        (slot-set! class 'hidden-slot-count (+ index 1))
        (list getter setter))

(define-class <foo> ()
   (x :allocation :hidden))
  :metaclass <foo-meta>)

(define-method initialize ((self <foo>) initargs)
  (slot-set! self 'values
             (make-vector (slot-ref (class-of self) 'hidden-slot-count)))

  Square USA Inc.   Honolulu Studio R&D division
#"The most important things are the hardest things to say" --- Stephen King
Received on Tue Jan 13 1998 - 22:45:59 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST