From: Mike Perkowitz
Subject: Initializing slots in CLOS
Date: 
Message-ID: <1992Jun11.192759.18221@cs.brown.edu>
I'm working on a system in CLOS where I need to have all the instances of a class
numbered so that each instance has a unique ID number. I'm doing this by
maintaining a slot called CurrentID that holds the number of the current 
highest ID number. CurrentID is an :allocation :class slot. Any time I make a 
new instance of the class, I want to increment CurrentID and set the ID number
of the new instance to the new CurrentID. So the first instance might be #1,
the second #2, and so on. The problem is that I don't know how to set up an
initform to actually read the current value of CurrentID and use it to 
initialize the ID number of the new instance. Any suggestions would be
greatly appreciated.

thanks,

Mike Perkowitz
From: Doug Cutting
Subject: Re: Initializing slots in CLOS
Date: 
Message-ID: <CUTTING.92Jun11125719@skye.parc.xerox.com>
In article <······················@cs.brown.edu> ···@cs.brown.edu (Mike Perkowitz) writes:

> I'm working on a system in CLOS where I need to have all the instances of a
> class numbered so that each instance has a unique ID number. I'm doing this
> by maintaining a slot called CurrentID that holds the number of the current
> highest ID number. CurrentID is an :allocation :class slot. Any time I make a
> new instance of the class, I want to increment CurrentID and set the ID
> number of the new instance to the new CurrentID. So the first instance might
> be #1, the second #2, and so on. The problem is that I don't know how to set
> up an initform to actually read the current value of CurrentID and use it to
> initialize the ID number of the new instance. Any suggestions would be
> greatly appreciated.

(defclass numbered () 
  ((current-id :allocation :class :initform 0 :reader current-id)
   (id :reader id)))

(defmethod initialize-instance :after ((object numbered)
                                       &key &allow-other-keys)
  (with-slots (id current-id) object
    (incf current-id)
    (setf id current-id)))