Path: utzoo!news-server.csri.toronto.edu!rutgers!uwm.edu!wuarchive!m.cs.uiuc.edu!cs.uiuc.EDU!johnson From: johnson@cs.uiuc.EDU (Ralph Johnson) Newsgroups: comp.lang.smalltalk Subject: Re: Smalltalk MetaClass questions. Message-ID: <1991Mar12.214143.10980@m.cs.uiuc.edu> Date: 12 Mar 91 21:41:43 GMT References: <9103111620.AA01484@sun.soe.clarkson.edu> Sender: news@m.cs.uiuc.edu (News Database (admin-Mike Schwager)) Reply-To: johnson@cs.uiuc.EDU (Ralph Johnson) Organization: University of Illinois Lines: 61 Here are the rules that I use to understand the subclasses of Behavior. 1) Everything in Smalltalk is an object. 2) Every object is a class. Obviously, classes must be objects, too. Therefore, they must have classes. So, what is the class of Array, or View, or Object? There are two possibilities: all classes can be instances of a single class Class, or different classes can have different subclasses. In order to reduce confusion, I'll call the class of a class a metaclass. Thus, I'll rephrase this as: there is only one metaclass, or different classes can have different metaclasses. It is a lot simpler if all classes had the same class, i.e. if there was only one metaclass, but that is not the way Smalltalk did it. If it had, Class would be the only metaclass, and in fact it wouldn't even be worth calling it a metaclass. If it had, Smalltalk wouldn't have class methods, and class methods are useful (though not necessary). 3) Every (nonmetaclass) class has its own metaclass. Smalltalk not only lets every class have its own metaclass, it forces each class to have its own metaclass. Metaclasses don't have their own names, thus the name of the class of Array is 'Array class'. All metaclasses are instances of Metaclass, thus 'Array class class' is Metaclass. Pretty boring. Thus, class can be divided into regular classes, which each have their own metaclasses, and metaclasses, which each are instances of Metaclass. 4) The metaclass of a superclass is the superclass of a metaclass. This seems pretty confusing when you say it in English, but it makes a lot of sense in a picture. Basically, it means that since Set is a subclass of Collection, Set class is a subclass of Collection class. There is a general rule that 'If X is a Y then the class of X is a subclass of the class of Y or one of its subclasses'. Thus, since Array is a class then the class of Array is a subclass of Class or one of its subclasses. By the same logic, the class of Object is a subclass of Class or one of its subclasses. We know that Object has no superclasses. However, we just said that Object class is a subclass of Class. Thus, we might as well make Object class be a direct subclass of Class. This results in some funny code, since the methods you see in the 'instance' side of the browser on Class get inherited by the 'class' side of the browser on all classes (including Class!). One of the interesting things about the class/metaclass notion is that it is completely invisible to the interpreter. As far as the interpreter is concerned, only Behavior (the common superclass of Class and Metaclass) is real. The only thing the interpreter does with classes is look up methods in their method dictionary, and any kind of Behavior does that just fine. Thus, the class/metaclass notion is defined entirely by the programming environment of Smalltalk, and not the interpreter. Thus, if you don't like it, you could change it! (Good luck!!!!) Ralph Johnson -- University of Illinois at Urbana-Champaign