The Dialectic of Classes and Metaclasses
in Smalltalk-80

The Smalltalk-80 programming language is known to support the "Everything is an object" paradigm. This paradigm manifests itself in particular by representing classes as objects: a class is also an object. Furthermore, objects are considered to be instances of classes which in turn are instances of metaclasses. However, which objects exactly are classes and which not?

This document collects evidence that most (if not all) publications about the Smalltalk-80 object model are inconsistent in the class–metaclass terminology. It is shown on a sample structure that the literature uses equivocal terminology that supports two different delimitations of classes and metaclasses.

Preface

Author
Ondřej Pavlata
Czech Republic
Document date
Initial releaseMarch 12, 2015
Last major release March 12, 2015
Last updateMarch 12, 2015
(see Document history)

Table of contents

Introduction

In object-oriented programming (OOP), there is a fundamental relation which expresses similarity between objects. Let us call this relation is-a or is-kind-of and denote ϵ. If x ϵ A and y ϵ A then x and y are both kind-of A so that they can share behavior described in (by) A. In the general context of OOP, objects only appear on the left side of the relation. The entities on the right side are not necessarily referenceable as objects. (So that if x ϵ A then x is an object but A is not in general.)

The ϵ relation has the semantics of set membership, . The inheritance relation, , corresponds to set inclusion, . It is defined between entities that appear on the right side of ϵ. In principle, is derived from ϵ by

just like is derived from . (By in principle we mean that we have to assume that A a B are distinguishable via their ϵ-preimage. Note that this is not the case of A and B from our sample structure.)

Since Smalltalk supports the Everything is an object pattern, the ϵ relation is a relation between objects. This is the main uniformity of Smalltalk: objects are described by other objects. The describing objects can be called meta-objects. The inheritance relation determines the precedence of meta-objects. Every object has a most specific meta-object – the least in inheritance, that one which has the highest precedence. The class method is the introspection method that returns this most specific meta-object.

However, Smalltalk-80 implements the ϵ relationship non-uniformly:

The property (1) would not cause a problem as such as long as it has been treated as an implementation feature. Unfortunately, in this respect, Smalltalk literature, together with the pair of introspection methods class / isMemberOf: mistakes implementation for concept, so that the non-uniformity of implementation induces a non-uniformity of the model. This is in turn compensated by terminological equivocation which tries to hide the non-uniformity.

One can also say that the implementation of ϵ introduces another kind of uniformity that is at odds with the conceptual uniformity of ϵ. Typically, conceptual uniformity of ϵ is proclaimed first ((A)-conformant statements), followed by nice statements about implementation ((B)-conformant statements). The tendency of (A) preceding (B) can be observed in the provided lists of excerpts.

Sample structure

Consider the sample core structure of the Smalltalk-80 object model shown by the following diagram.
Q: Which of the 18 objects are classes (metaclasses)?
(Pharo 1.3 / Squeak 4.2)
 r := ProtoObject.
 o := Object.
 b := Behavior.
cd := ClassDescription.
 c := Class.
mc := Metaclass.
Object subclass: #A.
A      subclass: #B.
u := B new.
v := B new.

Green links (with implicit upward direction) show inheritance between objects as reflected by the superclass introspection method. Blue links correspond to the class introspection method. The set of 18 objects is partitioned into metalevels 0, 1 and 2 according to the reachability of the object denoted mc (and named Metaclass) via blue links:

The class–metaclass dilemma

Given the sample structure, we want to resolve the precise meaning of the terms class and metaclass according to publications about Smalltalk-80. We want precise answers to the following 2 questions: This document provides an investigation that shows that the Smalltalk-80 literature supports (at least) two different answers simultaneously:
(A) Classes are the objects from metalevels 1 and 2.
Metaclasses are the objects from metalevel 2 plus the object named Metaclass from metalevel 1.
(B) Classes are the objects from metalevel 1.
Metaclasses are the objects from metalevel 2.
We introduce four types of statements. Neutral statements are those which conform both to (A) and (B). (C)-conformant statements conform neither to (A) nor to (B). The remaining statements are either (A)-conformant or (B)-conformant according to which of (A) or (B) they conform.
Neutral statements
(N1) No object from metalevel 0 is a class or metaclass.
(N2) All objects from metalevel 1 are classes.
(N3) All objects from metalevel 2 are metaclasses.
(N4) Every object is an instance of some object.
(N5) Every class is an instance of a metaclass.
(N6) Every instance of a metaclass is a class.
(N7) The instance-of relation is precisely what is displayed by blue links. Using introspection methods class and isMemberOf:, the following are equivalent for every objects x, y:
  • x is an instance of y.
  • x class == y (evaluates to true).
  • x isMemberOf: y (evaluates to true).
In particular, an object cannot be an instance of more than one object.
(N8) The is-a relation is the composition of instance-of with inheritance. The following are equivalent for every objects x, y:
  • x is a y.
  • x class is an inheritance descendant of y.
  • x isKindOf: y.
As a consequence,
  • x is from metalevel 1   iff   x is-a Class,
  • x is from metalevel 2   iff   x is-a Metaclass.
(A)-conformant statements
(A1) Every object is an instance of a class.
(A2) Every class is an instance of a class.
(A3) Every metaclass is a class.
(A4) The object named Metaclass is a metaclass.
Note that using the neutral statements as assumptions we obtain the equivalences In particular, an object is a class (resp. metaclass) iff it is reachable from the metalavel 1 via zero or more (resp. one or more) blue links.
(B)-conformant statements
(B1) There is a one-to-one correspondence between classes and metaclasses.
(B2) Inheritance of metaclasses parallels inheritance of classes.
(B3) Each class has a name. In contrast, metaclasses do not have names.
(B4) Every metaclass has exactly one instance.
(B5) Every metaclass inherits from Class.
(B6) The metaclass r class is the root of the hierarchy of metaclasses (where r denotes the inheritance root).
(B7) Every class is-a Class.
(B8) Every metaclass is an instance of Metaclass.
(B9) Metaclass is not a metaclass
  • (B9a) since it has multiple instances,
  • (B9b) since it has a name.
(B10) If some property P holds for both classes and metaclasses, then it is not sufficient to say that P holds for all classes.
(B11) Every class has its own metaclass.
(C)-conformant statements
(C1) The object named Class is a metaclass.

Excerpts from books

Pharo by Example []
Indicated Excerpt Page
(N7)(A1) Every object is an instance of a class. Every object has a class; you can find out which by sending it the message class. 80
(N7) … you can ask if an object is an instance of a specific class: isMemberOf:169
(N8) … there is a message, isKindOf:, that you can send to any object to find out if it is in an is a relationship with a given class. 280
(N5) Every class is an instance of a metaclass. 280
(B2) The metaclass hierarchy parallels the class hierarchy. 281
(a) Every metaclass is-a class, hence inherits from Class. 283
(B5) Every metaclass inherits from Class and …. 288
(B7) Every class is a Class. 288
(A3) … metaclasses are classes too … 288
(B8) Every metaclass is an instance of Metaclass. 288
(B10) The metaobjects in Smalltalk are classes, metaclasses, … 289
Introspection is the ability to examine the data structures that define the language, such as objects, classes, … 290
(N8) isKindOf: aClass returns true if the receiver is instance of aClass or of one of its superclasses. 290
(B10)(b) … classes and metaclasses have the common superclass Behavior. 297
… explore objects, classes and methods. 300

Notes:

Smalltalk and Object Orientation []
Indicated Excerpt Page
(A3) A metaclass is a class … 269
(B4) A metaclass has only one instance (the class it defines). 269
(B2) … the metaclass hierarchy was constrained to mirror the class hierarchy. 270
Class, ClassDescription and Behavior are not metaclasses (…). They are in fact classes, … 272
(B8) All metaclasses are instances of Metaclass. 273
(B10) … the structure of classes and metaclasses are very similar. 274
(B10) All things in the whole of Smalltalk are objects (…) whether they are class, metaclasses or ordinary instances. 274
Smalltalk: An Introduction to Application Development using VisualWorks []
Indicated Excerpt Page
(B4)(A3) The solution adopted in Smalltalk-80 is to have every class the only instance of another class, termed metaclass. 281
(B8) Every metaclass is of course an instance of a class, but as all metaclasses behave identically they are instances of the same class, which is called Metaclass. 281
(B1) Metaclasses are in one-to-one correspondence with classes. 281
(B9a) Metaclass is a class with multiple instances. 281
(B2) The general rule is that, if x is a subclass of y then x class is a subclass of y class. 282
(B2) … there are two parallel hierarchies: one for classes and one for their metaclasses. 282
An object is a class if and only if it can create instances of itself. 282
(B10) Because classes and metaclasses are very similar, Class and Metaclass are both subclasses of … 283
Smalltalk-80: The Language and Its Implementation []
Indicated Excerpt Page
(A1) Every object in the Smalltalk-80 system is an instance of a class. 8
(B3) Each class has a name. 40
Also, metaclasses do not have class names. 77
Since classes are components of the Smalltalk-80 system, they are represented by objects. 40
(A3) A class whose instances are themselves classes, is called a metaclass. 76
(B11) Whenever a new class is created, a new metaclass is created for it automatically. 77
(B8) Metaclasses are different from other classes because they are not themselves instances of metaclasses. Instead, they are all instances of a class called Metaclass. 77
(B5) Class is an abstract superclass for all of the metaclasses. 78
(B1) … there is a one-to-one correspondence between a class and its metaclass, … 78
(B2) The metaclass subclass hierarchy was constrained to be parallel to the subclass hierarchy of the classes that are their instances. 81
(B4)? A metaclass typically has only one instance. 81
(B2) If one class is a subclass of another, its metaclass will be a subclass of the other's metaclass. 83
(N5) Every class is an instance of a metaclass. 269
(B8) Every metaclass is an instance of Metaclass. 270
Smalltalk, Objects and Design []
Indicated Excerpt Page
(A2) … every class … is itself an instance of some class, which happens to be called its metaclass. 43
(B8) All the metaclasses are instances of one and the same class, whose name is, naturally enough, Metaclass. 43
(B1) If your system has 2000 ordinary classes, …, then it also has 2000 metaclasses. That's just 4000 class-like objects in all. 43
(B4) Every class is the only instance of its metaclass. In other words, each metaclass has exactly one instance. 257
Metaclasses have no names. 257
(B9b) Metaclass has a name. It is an ordinary class! 258
(B2) Whenever A is a subclass of B, A's metaclass is also a subclass of B's metaclass. 259
(B2) Inheritance of metaclasses parallels inheritance of classes. 259
(B2) The superclass of the metaclass is the metaclass of the superclass. 259
Behavior … gathers all the behavior that we would expect for class-like objects. 262
(B10) Even an object like class or metaclass is an instance of some class. 262
(B10) Any class-like objects, whether a class or metaclass, … 262
Inside Smalltalk, Volume I []
Indicated Excerpt Page
(A1) … every object in the Smalltalk system is an instance of some class. 40
(A2) … classes are objects and therefore must be instances of some class. 105
(B4) A class is the only instance of its own metaclass. 105
(B2) It is easiest to think of the class hierarchy as lying in the foreground; the metaclass hierarchy is a parallel hierarchy lying in the background. 234
There is no third layer in the background constituting meta-metaclasses. There are only classes and metaclasses - the buck stops at metaclasses. 235
Smalltalk-80 []
Indicated Excerpt Page
(A3)(??) The metaclasses are classes and therefore have a superclass called Class. 12
Objects contained in a class are called instances of this class. 31
(N7) class when sent to an object returns the class of that object. 31
(N7) isMemberOf: aClass allows one to find out if the receiver is a direct instance of aClass 31
(N8) isKindOf: aClass allows one to find out if the receiver is an instance of the argument aClass or of one of its subclasses. 31
(A1)(A2) (N7) We know that each object is an instance of a class. As a class is itself an object, it is an instance of a class that we call its metaclass. To access the metaclass of a class we send it the message class. 40
(B2) The hierarchy that appears at the level of the metaclasses is the same as that which appears at the level of the classes; that is, if a class A is a subclass of a class B, the metaclass will be a subclass of the metaclass of B, … 40
(B4)(B1) Each metaclass has only one instance and thus there are exactly as many metaclasses as classes. 40
(B8) The metaclasses are also objects and thus belong to a class that is called Metaclass. Metaclass is a class and thus has a metaclass which is an instance of Metaclass. Metaclass and its metaclass are both instances of each other. 40
(B8) The metaclasses are instances of the class Metaclass. 50
Object-Oriented Programming with C++ and SmallTalk []
Indicated Excerpt Page
(B3) Unlike class objects, metaclass objects are not named as global variables. 475
(N7)(N6) (A1) We obtain a reference to a metaclass object by passing the message class to its instance, the class object, just like accessing the class of any object. 475
(B6) The metaclass Object class is the root of the hierarchy of metaclasses, and the hierarchy of metaclasses descended from Object class parallels that of the classes descended from Object. 478
(N7) Because Metaclass is the instance of Metaclass class this is the point of circularity in the system with respect to class (i.e., "instance of") references. 479
(A1) … every object is an instance of a class, … 479
(B4) Every class object is the single instance of a metaclass (of which there is a single instance), … 479
(B5) … all metaclasses are subclasses of Class. 479
(B8) Every metaclass is an instance of Metaclass, including Metaclass class. 479
(B8) All metaclasses are instances of the system class Metaclass, …. This includes Metaclass class since Metaclass is a class object. 485
(N5)(B4) Each class object is an instance of a metaclass that defines its state and behavior, and the class reference of a class object points to its metaclass object. 485
(B11)(B4) Each class is an instance of its own metaclass, so that …, and each metaclass has a single instance. 485
Programming Smalltalk–Object-Orientation from the Beginning []
Indicated Excerpt Page
(A1) … classes are objects and, conversely, each object is an instance of a class … 269
(B4) … each class is the sole instance of its metaclass. 269
(B3) … metaclasses do not have their own names. 270
(B2) If class A is a subclass of class B, an analogous relationship holds true for their metaclasses. 270
(B8) The class Metaclass is the class of all metaclasses. 270

Excerpts from papers

Smalltalk: a Reflective Language []
Indicated Excerpt Page
(A1) Each object is an instance of a class that describes both the behavior and the structure of its instances. 4
Classes as regular objects are described by other (regular) classes called metaclasses. 4
(B4)(??) A metaclass has a single instance (except metaclasses involved in the kernel of Smalltalk). 4
(B2) Inheritance on metaclasses follows the one at the class level … 4
(B11) (C1)(A4) The behavior of classes and metaclasses are described by two (meta)classes respectively named Class and Metaclass. 4
(B8) All metaclasses are instances of Metaclass, … 4
(A3) Metaclass definition: classes whose instances are classes themselves. 4
Language support for Adaptive Object-Models using Metaclasses []
Indicated Excerpt Page
(A3) … it is often practical to transfer the qualification of instances to their classes: accordingly we shall speak of a terminal class to mean a class whose instances are terminal objects, as opposed to a metaclass, whose instances are classes. 7
(B11) With each class C a metaclass called "C class" is automatically associated, of which C is the only instance. 9
If class B is a subclass of C, then its associated metaclass "B class" is automatically a subclass of metaclass "C class". 9
(B2) The system manages the metaclass inheritance hierarchy and makes it be parallel to the class inheritance hierarchy. 10
(C1)(A4) The Smalltalk-80 model of implicit metaclasses supports … the Class metaclass, and the Metaclass metaclass. 10
(B5) Class is the root of metaclasses that describe the structure and behavior of classes. 10
(??) Metaclass is the root of the meta-metaclass hierarchy that describe the structure and behavior of implicit metaclasses. 10
A Tutorial Introduction to Metaclass Architecture []
Indicated Excerpt Page
(A1) Each object belongs to a single class. 4
(B11) Classes are also objects defined as the sole instance of their metaclasses. 5
(B2) … the inheritance of metaclasses is parallel to the inheritance of classes: if a class is a subclasss of another one, then its metaclass is a subclass of the metaclass of the other. 5
(B6) The root of the metaclass's inheritance tree is the class Object class defined as a subclass of Class. 5
(B8) Metaclasses are equally objects, instances of Metaclass. 5
Classes and Metaclasses in Smalltalk (e-mail) []
The following are excerpts from an e-mail message by the inventor of implicit metaclasses in Smalltalk-80.
Indicated Excerpt
(A3)
  • a class is an object that can be instantiated
  • a metaclass is a class and one such that when it is instantiated the instanced is itself a class.
  • a plain-object is one that cannot be instantiated
  • a plain-class is one that is a class but not a metaclass
(B8) All metaclasses are instances of Metaclass.
All plain-classes (those that are not metaclasses) are instances of a metaclass.
Because of this there are parallel class hierarchies between plain-classes and their corresponding metaclasses.
(B9) Note that Metaclass is a plain-class and not a metaclass.

Resolving the dilemma

Since (A) and (B) are in a mutual contradiction, any consistent terminology can stick to at most one of these answers.
Sticking to (A)
Adhering to (A) requires an adjustment of all (B)-conformant statements. We show how such an adjustment can be made based on (an alteration of) the terminology made up in the e-mail by James Althoff. []

Let us say that an object can be instantiated iff it is not from the metalevel 0.

Of course, plain-metaclasses are exactly the objects from metalevel 2 and the only non-plain metaclass is the Metaclass class. Plain-classes are exactly the objects from metalevel 1. Given that, all the (B)-conformant statements are made consistent with (A) by the following replacements:
Sticking to (B)
Adherence to (B) has the following disastrous consequences to the uniformity of the Smalltalk-80 object model:
(A1') Not every object is an instance of a class.
(A2') A class is never an instance of a class.
(A3') No metaclass is a class.
(A4') Metaclass is not a metaclass.
Moreover, if x class == y then one cannot say in general that y is the class-of x since y is not necessarily a class.
A retrofit from Ruby
Another solution is to treat the metalevel 1 as a classification system, similarly to the Ruby programming language. This yields the following alternative to (A) and (B):
(C) Classes are the objects from metalevel 1.
Metaclasses are either implicit or explicit.
  • Implicit metaclasses are the objects from metalevel 2.
  • Explicit metaclasses are the two classes named Class and Metaclass.
As a consequence, there is a one-to-one correspondence between classes and implicit metaclasses.

Based on inheritance () and the is-a relation (ϵ, introspected via isKindOf:), the class-of an object x is the least class y such that x ϵ y. As a consequence, the class of x equals

The instance-of relation has to be altered to be correspondent with the class-of map. Delimitation of metaclasses is obtained as the minimum solution of the following requirements: Let C denote the set of all classes according to (C), .ȼlass the (redirected) class-of map as described above, and . the down-set closure operator w.r.t. inheritance. Then the set of all metaclasses can be described as C.ȼlass., i.e. metaclasses are the (non-strict) inheritance descendants of (redirected) classes of classes. []

 

References
James Althoff, [Python-Dev] Classes and Metaclasses in Smalltalk , 2001, https://mail.python.org/pipermail/python-dev/2001-May/014508.html
Johannes Brauer, Programming Smalltalk – Object-Orientation from the Beginning, Springer 2015,
Pierre Cointe, A Tutorial Introduction to Metaclass Architecture as provided by Class Oriented Languages, Proc. of the International Conference on Fifth Generation Computer Systems, 1988,
Caleb Drake, Object-Oriented Programming with C++ and SmallTalk, Prentice Hall 1998,
Adele Goldberg, David Robson, Smalltalk-80: The Language and Its Implementation, Addison Wesley 1983, http://stephane.ducasse.free.fr/FreeBooks/BlueBook/Bluebook.pdf
Trevor Hopkins, Bernard Horan, Smalltalk: An Introduction to Application Development using VisualWorks, Pearson Education 1995, http://stephane.ducasse.free.fr/FreeBooks/HopkinsHoran/HopkinsHoran.pdf
John Hunt, Smalltalk and Object Orientation: An Introduction, Springer Verlag 1997, http://stephane.ducasse.free.fr/FreeBooks/STandOO/Smalltalk-and-OO.pdf
Wilf R. LaLonde, John R. Pugh, Inside Smalltalk, Volume I, Prentice Hall 1990, http://stephane.ducasse.free.fr/FreeBooks/InsideST/InsideSmalltalk.pdf
Chamon Liu, Smalltalk, Objects and Design, toExcel 1999
A. Mével, T. Guéguen, M. Wolczko, Smalltalk-80, Palgrave Macmilla 1987
Oscar Nierstrasz, Stéphane Ducasse, Damien Pollet, Pharo by Example, Square Bracket Associates 2009, http://pharobyexample.org
Ondřej Pavlata, Object Membership: The Core Structure of Object Technology, 2012–2015, http://www.atalon.cz/om/object-membership/
Reza Razavi, Noury Bouraqadi, Joseph Yoder, Jean-François Perrot, Ralph Johnson, Language support for Adaptive Object-Models using Metaclasses , Computer Languages, Systems & Structures, 31(3) 2005,
Fred Rivard, Smalltalk: a Reflective Language , Proceedings of REFLECTION, Vol. 96, 1996,
Document history
March122015 The initial release.
License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.