From: Lars Rune Nøstdal
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <1227225960.16144.28.camel@blackbox.nostdal.org>
On Thu, 2008-11-20 at 15:09 -0800, Lars Rune Nøstdal wrote:
> err, ups .. correct version:
> 
> (let ((id-generators (make-hash-table :test #'eq :weakness :key)))
>   (defmethod generate-id-for (object)
>     (multiple-value-bind (id-generator found-p)
>         (gethash (class-of object) id-generators)
>       (if found-p
>           (funcall id-generator)
>           (prog1 0
>             (setf (gethash (class-of object) id-generators)
>                   (let ((id 0))
>                     (lambda () (incf id)))))))))


AMU> (defclass test () ())
#<STANDARD-CLASS TEST>
AMU> (generate-id-for (make-instance 'test))
0
AMU> (generate-id-for (make-instance 'test))
1
AMU> (generate-id-for (make-instance 'test))
2
AMU> (setf (find-class 'test) nil)
NIL
AMU> (defclass test () ())
#<STANDARD-CLASS TEST>
AMU> (generate-id-for (make-instance 'test))
0
AMU> (generate-id-for (make-instance 'test))
1
AMU> (generate-id-for (make-instance 'test))
2
AMU> 

if i just redefine the class (say, add slots to it or whatever) without
doing (setf (find-class 'test) nil) .. the "id counter" would not have
been "reset to 0"

the "point" (i think..) is that i do not want to think about having to
add a static slot to classes .. as in; i do not need to know or change
already existing code at all .. it works vs. built-in classes for
instance:

AMU> (generate-id-for 1234)
0
AMU> (generate-id-for 1234)
1
AMU> (generate-id-for 1234)
2
AMU> (generate-id-for 1234)
3
AMU> (generate-id-for 12)
4


AMU> (generate-id-for "a")
0
AMU> (generate-id-for "a")
1
AMU> (generate-id-for "b")
2
AMU> (generate-id-for "b")
3
AMU> (generate-id-for "b")
4
AMU> (class-of 1)
#<BUILT-IN-CLASS FIXNUM>
AMU> (class-of "a")
#<BUILT-IN-CLASS SB-KERNEL::SIMPLE-CHARACTER-STRING>