[plt-scheme] swindle clos question

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Feb 4 16:34:04 EST 2005

On Feb  4, Larry White wrote:
> I can't recall the term used in the clos world, but a variable that
> I intended to be an inst variable seems to be acting like a class
> var (oh yeah, class allocation, that's it).
> I have the following hierarchy (extra fields removed):  

[I've edited your code so it's easier to read.]

> (defclass section ()
>   (accumulators :accessor accumulators :initvalue (make-hash-table)))
> (defclass group (section))
> And the following methods
>   (defmethod (add-accumulator (a accumulator) (sect section) key)
>     (hash-table-put! (accumulators sect) key a))

I don't see any definition for `accumulator'.

> I create two groups and two accumulators (one for each) and assign
> them as so:
> (define g1 (make-group "status" "Status"))

What is `make-group'?  In Swindle, you'd use (make group <keyword+args>)

> (define ac1 (make-count "status"))

And what is `make-count'?

> (add-accumulator ac1 g1 'count-status)
> (define g2 (make-group "type" "type"))
> (define ac2 (make-count "type"))
> (add-accumulator ac2 g2 'count-type)
> What happens is that I seem to have a single hashtable in both groups.
> If I call the accessor (accumulators g1)  it has both entries and
> (eq? (accumulators g1) (accumulators g2)) returns # t 

Ignoring all the above, the problem is that you use `:initvalue' which
is evaluated once, and this value is used in all instances.  So you
get all instaces of this class using the same hash table.  You need to
use `:initializer' instead, for example:

  (defclass section ()
    (accumulators :accessor accumulators
                  :initializer (lambda () (make-hash-table))))

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.