From: Bob Manjoney
Subject: Finding CLOS instances
Date: 
Message-ID: <6vjprc$55u$1@ash.prod.itd.earthlink.net>
2 elementary CLOS questions:

1)  How does one find instances of a particular CLOS class for inspection?
I'm coming from Smalltalk, where the expression:

    someClass allInstances.

would return a collection of all instances of someClass.  Is there anything
comparable in CLOS?

2)  Is there a CLOS slot specifier that results in a slot whose scope is
analogous to that of a "class instance variable"(1)  in Smalltalk?  Or is
there some other mechanism which has the same net effect?


Thanks,
Bob Manjoney
McHugh Software



Taken from "The Smalltalk FAQ" at
http://www.cis.ohio-state.edu/hypertext/faq/usenet/smalltalk-faq/faq.html:

"Class instance variables are similar to class variables, except that
they are created for each subclass of the defining class. When a class
declares a class instance variable, a new variable is created for each
subclass of that class. Each subclass then has its own instance of the
variable and retains its own value for the variable, but each subclass has
a variable with the same name. Only class methods of a class and its
subclasses can refer to class instance variables; instance methods cannot."

From: Kent M Pitman
Subject: Re: Finding CLOS instances
Date: 
Message-ID: <sfwk92a77c9.fsf@world.std.com>
"Bob Manjoney" <·····@acm.org> writes:

> 2 elementary CLOS questions:
> 
> 1)  How does one find instances of a particular CLOS class for inspection?
> I'm coming from Smalltalk, where the expression:
> 
>     someClass allInstances.
> 
> would return a collection of all instances of someClass.  Is there
> anything comparable in CLOS?

You could make a metaclass that kept track of its own instances, but
for GC reasons classes do not normally track their instances.  Or you
could make a :AFTER method on something like INITIALIZE-INSTANCE that
did this, though you have the problem of where to store the info.  The
few times I've done this, I made a global hash table whose key was the
class and whose value was the instance list for that class.  Then have
the method push onto that list when a new instance is created.  Though
obviously you have the problem of emptying out the list when the
instance is dropped... CL doesn't (presently) have weak hash tables,
though I think it's within the realm of technology these days and
maybe we should consider requiring them.  It was something people knew
of when CL was initially designed, but I think people were ill at ease
about requiring them then.
 
> 2) Is there a CLOS slot specifier that results in a slot whose scope
> is analogous to that of a "class instance variable"(1) in Smalltalk?
> Or is there some other mechanism which has the same net effect?

No, and it would be nice to have.  We have :allocation :class, but
that is not the same thing.  Dylan refined this idea by providing what
it calls "each-subclass allocation" which is, I think, what you want
and what CL should add at its first opportunity.  (There would likely
be no restriction on "class methods" vs "instance methods", though.
Unless I misunderstand what you mean by that, CL and Dylan presently
make no such distinction/restriction.  If you'd like to argue there
should be such a restriction, I'd be interested to hear the
rationale.)

But you could simulate such a thing fairly straightforwardly.  (Well,
for varying values of "straightforward".  I've observed that some
people here don't routinely extend the language to suit their needs as
much as others do... :-)
From: Erik Naggum
Subject: Re: Finding CLOS instances
Date: 
Message-ID: <3116943851349974@naggum.no>
* "Bob Manjoney" <·····@acm.org>
| 1)  How does one find instances of a particular CLOS class for inspection?
| I'm coming from Smalltalk, where the expression:
| 
|     someClass allInstances.
| 
| would return a collection of all instances of someClass.  Is there anything
| comparable in CLOS?

  nothing in CLOS itself, but I needed this once, and discovered a function
  (present in Allegro CL 4.3, 4.3.1, and 5.0) that helps me get it:

(defun list-all-instances (class)
  (loop
    with instances = (excl::get-objects 12) ; standard-instance
    for index from 1 to (svref instances 0)
    for instance = (svref instances index)
    when (typep instance class)
    collect instance))

  (evaluate (print-type-counts) to learn that 12 is standard-instance.)

  hope this helps.

#:Erik