unom
unom

Reputation: 11486

How to get *all* classes in Pharo?

Conundrum... tried Smalltalk allClasses and TBehaviour in Kernel-Traits, among others, seems to be missing from the list. Interestingly enough it is a Trait not a Class...? there seem to be corresponding allTraits and allBehaviors.

Any others we should know about when trying to get everything? Or is there some other method to get everything?

Upvotes: 3

Views: 807

Answers (2)

Peter Uhnak
Peter Uhnak

Reputation: 10217

Note: I thought that reflection was described in some Pharo book but I don't see it in any, so can't direct you for further reading. 🤔

Classes are Objects

You can always use reflection on Pharo objects, which might give you a bit more insight into what you are actually looking for.

Any class is also an object, an any object understands message allSubclasses (or withAllSubclasses) that will give you... the subclasses.

Object willAllSubclasses

Note that the above will give you also the "class-side" classes (which are metaclass instances for each class), because they are objects too; so

Smalltalk allClasses asSet =¹ (ProtoObject withAllSubclasses \ Class allSubclasses) asSet
"or"
Smalltalk allClasses asSet = (ProtoObject withAllSubclasses \ Metaclass allInstances) asSet

Traits are not Classes

Trait is a class, but TBehavior is not; instead it is an instance of Trait.

So you can say

Trait allSubclasses. "an OrderedCollection()"
Trait allInstances. "{... TBehavior. TClass. ...}"

¹SMarkCompilerTargetClass is some special snowflake.

Upvotes: 3

EstebanLM
EstebanLM

Reputation: 4357

You need to execute:

Smalltalk allClassesAndTraits.

Upvotes: 3

Related Questions