[ By Archive-name | By Author | By Category | By Newsgroup ]
[ Home | Latest Updates | Archive Stats | Search | Usenet References | Help ]

    Search the FAQ Archives

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page

Top Document: FAQ: Object-oriented Programming in Lisp 5/7 [Monthly posting]
Previous Document: [5-2] How can I write a function that can access defstruct slots by name? I would like to write something like
Next Document: [5-4] How can I store data and CLOS instances (with possibly circular references) on disk so that they may be


[5-3] How can I list all the CLOS instances in a class?



There is no built-in way to enumerate the instances of a class.  If you are
only interested in listing the instances of classes that you have defined,
it is not very difficult to implement it as part of your class definition.
Add a shared slot, e.g. ALL-INSTANCES, with an initial value of NIL, to the
class definition.  Then write an after-method on INITIALIZE-INSTANCE for
this class, which pushes the instance being initialized onto ALL-INSTANCES.
Note that this must be done separately for each class that wants to maintain
such a list; it can't be encapsulated in a mixin class, because all its
dependent classes would share the same ALL-INSTANCES slot.  A compromise
would be to use a mixin to define the INITIALIZE-INSTANCE after-method (and
any other general-purpose methods that use the slot), but not the shared
slot; it would be up to the descendant classes to define the slot at the
level of the class hierarchy that is appropriate. You could also try
defining the classes that need instance-recording as instances of a
metaclass that holds the instance registry on the class object. The
recording behavior could then be built-in to an after method on
initialize-instance for the root class of the metaclass, or even
allocate-instance. To allow for garbage collection of old instances,
you will also need to define a generic function to remove the recorded
instances from the list of instances.



Top Document: FAQ: Object-oriented Programming in Lisp 5/7 [Monthly posting]
Previous Document: [5-2] How can I write a function that can access defstruct slots by name? I would like to write something like
Next Document: [5-4] How can I store data and CLOS instances (with possibly circular references) on disk so that they may be

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page


[ By Archive-name | By Author | By Category | By Newsgroup ]
[ Home | Latest Updates | Archive Stats | Search | Usenet References | Help ]


Send corrections/additions to the FAQ Maintainer:
ai+lisp-faq@cs.cmu.edu

Last Update July 06 2008 @ 00:12 AM

© 2008 FAQS.ORG. All rights reserved.