April 25, 2024

Metaclass

 

In the beginning there was Smalltalk and it was not so good. One problem was that classes were not objects. So the XeroxPARC people decided to go for consistency and make all classes objects. But it doesn't take too long to ask yourself that if classes are instances of some class (Class) then what is Class an instance of? Well Class was an instance of itself. But they ran into more problems because initialising objects of any class was difficult and so they trooped back to the drawing board.

The solution they adopted was to make any class the sole instance of its own metaclass. These metaclasses are in turn instances of a class called Metaclass.

However, if all metaclasses are instances of Metaclass, what is the metaclass of Metaclass? Which class is it an instance of?

To solve this one you can do a test in LW.
To answer the metaclass of any class simply send the message class to the class
E.g.
Account class
will answer with Account class and using the inspector will confirm that this object is an instance of Metaclass.
Now evaluate
Metaclass class
This will answer with Metaclass class, which is an instance of Metaclass. Now send class to this object by evaluating
Metaclass class class
and lo and behold it answers with Metaclass.
Or you could evaluate
Metaclass class class == Metaclass
This answers true, thus proving that the metaclass of Metaclass' metaclass is Metaclass (I know, it's a tongue twister). It is an instance of itself; the loop has been neatly closed.

When you create a class, you go on to specify instance methods for that class so that any instance of that class inherits behaviour that is common to all instances of that class. The same goes for Metaclass, since it will determine behaviour that is common to all its instances (metaclasses). Metaclass is a subclass of ClassDescription and this in turn is a subclass of Behavior. In Behavior you will find the messages new and new: (used to create arrays as you will discover later). Behavior is a subclass of Object. If you evaluate
Behavior printHierarchy
you will see the truth.

This is as much detail as I wish to go into. M206 students won't be examined on metaclasses, but I think that it helps to see the consistency of Smalltalk and that everything is either an object or a message.

« Back to FAQ page

« Back to class methods

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page