Reflection about allInstances
Andreas Raab
andreas.raab at gmx.de
Thu Sep 7 23:30:21 UTC 2006
A correct observation but anyone for whom this matters understands (or
will learn very quickly) that #allInstances is a utility method that
cannot possibly give the correct answer in all situations. You have
found one, but there are others (hint: think about method activation).
The only true way to find all instances of some class is to do it
yourself at the place where you need it, like here:
obj := aClass someInstance.
[obj == nil] whileFalse:[obj := obj nextObject].
Cheers,
- Andreas
Mathieu wrote:
> Hi,
>
> I was thinking about what happen if we do:
>
> Array allInstances
>
> Beceause #allInstances return an Array but this array are not include in himself.
> So #allInstances don't really return all instances?
>
> So after I have investigated allInstances and have discovered that the array was created at the end
> of the method. But an other question come out.
>
> I have saw in the object return by 'Array allInstances' that at the end many array look like the one
> return by 'Array allInstances'.
>
> Hope you follow up to now. :)
>
> So I was wondering why. And I have look the implementation of OrderedCollection and saw that it use
> Array to store the elements.
>
> So If we don't want the element create during the execution of allInstances, the one inside the
> OrderedCollection shouldn't appear?
>
>
> | all |
> all _ OrderedCollection new.
> self allInstancesDo: [:x | x == all ifFalse: [all add: x]].
> ^ all asArray
>
>
> "x == all" it's not enough. It should be something like :
>
> "all allDeepInstVar includes: x" (#allDeepInstVar dose not exist)
>
> I know that may not be very important but what do you think?
>
> Math
>
>
>
>
More information about the Squeak-dev
mailing list
|