Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
32. The Metaclass Framework<br />
32.1 <strong>Introduction</strong><br />
The metaclass concept is probably one of the most confusing parts of the whole of <strong>Smalltalk</strong>. This is<br />
partly due to the very confusing names used (for example, Class class <strong><strong>an</strong>d</strong> Metaclass class)<br />
but also because almost all of it is hidden from the developer. You are therefore only vaguely aware of<br />
it (if you are aware of it at all) during development. This me<strong>an</strong>s that developers do not need to<br />
underst<strong><strong>an</strong>d</strong> it in detail <strong><strong>an</strong>d</strong> most people avoid trying to underst<strong><strong>an</strong>d</strong> what appear s to be a difficult concept<br />
if they don’t have to. It also me<strong>an</strong>s that developers c<strong>an</strong> only grasp what is happening abstractly, with<br />
little ch<strong>an</strong>ce to see the mech<strong>an</strong>ics of what is going on.<br />
However, contrary to popular myth, the metaclass concept is actually not that difficult (if you c<strong>an</strong><br />
get around the terminology), although it c<strong>an</strong> be confusing especially when its recursive definitions are<br />
encountered. We will however leave that aspect of metaclasses until right at the end. This me<strong>an</strong>s that<br />
you should hopefully gain at least <strong>an</strong> appreciation of what metaclasses about, even if the fine details of<br />
the implementation pass you by. In m<strong>an</strong>y ways, it is only <strong>an</strong> appreciation that you need (if you need it at<br />
all) to underst<strong><strong>an</strong>d</strong> what you observe in the development environment.<br />
As the basic concept being considered in this chapter is the concept of a class, Section 2 provides a<br />
review of what is me<strong>an</strong>t by a class etc.<br />
Metaclasses have not always been present in <strong>Smalltalk</strong>, they were only introduced into <strong>Smalltalk</strong><br />
with the dev elopment of the <strong>Smalltalk</strong> -80 version of <strong>Smalltalk</strong>. This is essentially the version of<br />
<strong>Smalltalk</strong> which is used as the basis of commercial versions of the l<strong>an</strong>guage (such as VisualWorks).<br />
Prior to that a number of (simpler) approaches were used. However, each of these provided fundamental<br />
problems which were overcome with the introduction of metaclasses.<br />
Section 3 discusses the metaclass mech<strong>an</strong>ism <strong><strong>an</strong>d</strong> how it works. As was stated before, it does this<br />
without going into the complexities of the implementation of metaclasses. This allows the casual reader<br />
to underst<strong><strong>an</strong>d</strong> where metaclasses fit in <strong><strong>an</strong>d</strong> what they do without needing to underst<strong><strong>an</strong>d</strong> the intricacies of<br />
the full metaclass inherit<strong>an</strong>ce hierarchy.<br />
The classes ClassDescription <strong><strong>an</strong>d</strong> Behavior are introduced in Secti on 4. These classes<br />
encapsulate what it me<strong>an</strong>s to be a class <strong><strong>an</strong>d</strong> how classes do what they do. Section 5 considers the<br />
relationship between metaclasses, classes <strong><strong>an</strong>d</strong> the Metaclass class. This is not actually that complex, but<br />
c<strong>an</strong> appear confusing at first sight.<br />
A point to note before reading this chapter are the conventions used for <strong>Smalltalk</strong>, classes <strong><strong>an</strong>d</strong><br />
inst<strong>an</strong>ces. Remember:<br />
• All <strong>Smalltalk</strong> code appears in a courier font, e.g. Collection new.<br />
• All classes start with a capital letter e.g. Set, <strong>Object</strong>, Array.<br />
• All inst<strong>an</strong>ce names start with a lower case letter, e.g. aSet, <strong>an</strong><strong>Object</strong>, <strong>an</strong>Employee.<br />
These conventions are adhered to rigidly below.<br />
32.2 What are classes?<br />
All classes are ultimately subclasses of the class <strong>Object</strong> (with the exception of <strong>Object</strong> itself). That<br />
is, a class inherits properties form its superclass (which in turn inherits properties form its superclass)<br />
up to <strong>Object</strong>. In turn every thing in <strong>Smalltalk</strong> is <strong>an</strong> object <strong><strong>an</strong>d</strong> every object is <strong>an</strong> inst<strong>an</strong>ce of a class.<br />
On page 269 of the Purple Book [Goldberg <strong><strong>an</strong>d</strong> Robson 1989] the following statement is made:<br />
268