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 See reader questions & answers on this topic! - Help others by sharing your knowledge 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. User Contributions: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 [ Usenet FAQs | Web FAQs | Documents | RFC Index ] Send corrections/additions to the FAQ Maintainer: ai+lisp-faq@cs.cmu.edu
Last Update March 27 2014 @ 02:11 PM
|
Comment about this article, ask questions, or add new information about this topic: