This section takes an entirely optional glimpse at an advanced topic that Chamond Liu treats fully in Chapter 20 of Smalltalk Objects And Design.
One of the consequences of admitting classes as first-class objects is that classes themselves must then be instances of some other class. In effect, a factory must come from some sort of higher-level factory. Thus every class, like Whale, is itself an instance of some class, which happens to be called its metadass. Moreover, class Whale happens to be the one and only instance of its metaclass. As a matter of fact, every class is the one and only instance of its own metadass.
Now, if every class has an associated metaclass, and the class is the only instance of this metaclass, that doubles the number of class-like objects running around the system. You’re probably fearing the worst – that this goes on forever, with meta-meta-classes and so on. Fortunately not. The next level is much simpler.
All the metadasses are instances of one and the same class, whose name is, naturally enough, Metaclass. The explosion of class-like objects stops cold with this one class. You can even count them all up. If your system has 2000 ordinary classes – mind you, that's really 1999 plus one called Metaclass – then it also has 2000 metaclasses. And all 2000 of these metaclasses are instances of Metaclass. That's just 4000 class-like objects in all.
Now, 4000 is 2000 more than a class browser really needs to make you aware of. The class browser is a practical tool and was designed to conceal those 2000 meta-classes. How? By giving you the convenient toggle you saw that lets you look at “class methods.” The so-called class methods of Whale are actually the instance methods of Whale's metaclass! By this sleight-of-hand, the Smalltalk browser conceals Whales metaclass, and portrays its methods to us as an artificial breed of method called “class methods."
You can program for a long time in Smalltalk without knowing as much as I've already said about metaclasses. The noteworthy theme is that metaclasses preserve the conceptual consistency of Smalltalk. (“Everything is an object, and everything is therefore also an instance of some class.“) This consistency is unlike what you'll find in most other object-oriented languages. C++ classes are not objects; they aren't eligible to receive messages, for example. C++ classes are limited to the role of describing the system rather than participating in it.
Again, the details of the metaclass story appear in Chapter 20. Meanwhile, the next step is to begin programming in Smalltalk.
CHAPTER 20
We have now learned about polymorphism, patterns, frameworks, and the rest of the customary object-oriented topics. This brief chapter offers a respite from those main stream topics and a final excursion for readers who are curious about just how far Smalltalk goes to celebrate its consistent view of objects. Along the way it answers a simple but perplexing question; "Where is method new?" It is possible to program competently in Smalltalk for years without understanding the answer, but the answer is an opportunity to discover the abstruse world of *metaclasses*.
[…]
Each metaclass has exactly one instance; these instances are classes and are shaded to distinguish them from ordinary instances. From bottom to top they are Penguin, Bird, Animal, and Object. Notice the appearance of a class named Class that conveniently contains all the class objects. The picture shows that every class is a Class, which is a reassuring but unremarkable fact.
~
You encountered the class named Class in another context, while solving the exercise on object memory layouts on page 195.
~
# Metaclass Objects Are Striped
Finally, the metaclasses are objects in their own right, and they all reside in a class named Metaclass. You can think of the metaclass objects depicted from bottom to top as Penguin class, Bird class, Animal class, and Object class.
And here is the result of assembling all these schematics (*Schaltpläne*) into a whole:
One of the side effects of this diagramming technique is that if all […] classes […] were represented in one diagram, each class would appear twice, once as a rectangle to indicate its subclass/superclass relationships and once as a shaded circle to indicate what it is an instance of. Similarly, every metaclass would appear twice, once as a rectangle and once as a striped circle.
# Behavior
Notice that one additional class has appeared — Behavior. Behavior is a superclass of both Class and Metaclass. As its name and position in the hierarchy suggest, it gathers all the behavior that we would expect for class-like objects. Behavior is in fact the answer to the question that began this chapter: the default method new is an instance method in Behavior. A glance at the diagram shows that this is an excellent location for new. For if none of the animal metaclasses implements a new method, inheritance up the metaclass hierarchy in the center of the diagram shows that the lookup for the message Penguin new will eventually arrive at Behavior, where the default new will execute. In addition to new, what other instance methods could plausibly reside in Behavior? Well, any method that makes sense for all the class-like objects. By browsing class Behavior you will find alluring methods like allSubclasses, allSuperclasses, instVarNames, methodDictionary, as well as ones you've already used like new and allInstances.
[…]
~
Chamond Liu, Smalltalk, Objects, and Design: The Classic Guide, Now in Paperback, Reprinted from the original 1996 ed. with minor corrections (San Jose: toExcel, 2000), p. 43 and 257–262.
=> Re-entry