Metaclass

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses. Among those that do, the extent to which metaclasses can override any given aspect of class behavior varies. Each language has its own metaobject protocol, a set of rules that govern how objects, classes, and metaclasses interact.[1]

Contents

Alternative definition[edit]

The classical definition of a metaclass as a class whose instances are classes poses several problems:

  • All classes without instances would pass the definition, so that they become metaclasses by vacuous truth.
  • If the "instance-of" term is used in the non-strict sense, allowing indirect instances [a], the inheritance root can be wrongly recognized as a metaclass.
  • The requirement that a metaclass is a class disallows (or at least complicates) a uniform view of the Ruby and Smalltalk-80 object model.

The following alternative definition ,[2] based on the eigenclass model, avoids all the above problems.

  1. The metaclass root is the class of the inheritance root.
  2. An object is a metaclass if it is an inheritance descendant of the metaclass root.

In (2), inheritance descendant is meant in the non-strict sense – including the metaclass root itself. The definition applies to all of Ruby, Python, Scala, Java, Smalltalk and CLOS, although in the case of the latter two languages care must be taken as to what is meant by class of in (1). Unfortunately, the introspection methods class (in Smalltalk) and class-of (in CLOS) are misleading here.

For Objective-C, a modified definition has to be used due to the multiplicity and degeneracy of inheritance roots.

Metaclass root[edit]

The metaclass root is simultaneously the instance root – it is the only object that is the class of itself.[dubious ] (For Smalltalk and CLOS, the semantics of "class of" in the above sentence differs[clarification needed] from the semantics of the similarly named introspection method). In Objective-C, an object is a class of itself if and only if it is a metaclass root.

By standard, the metaclass root is named Class. A notable exception is that of Python where it is named type. In Objective-C, metaclasses are not named.

Explicit versus implicit metaclasses[edit]

Metaclasses are either explicit or implicit according to whether they are classes or not, respectively. The metaclass root is a built-in explicit metaclass. Of the above mentioned languages, only Python and CLOS support other explicit metaclasses. On the other hand, implicit metaclasses are supported by Ruby, Smalltalk-80 and Objective-C. In Java and Scala, the metaclass root is the only metaclass.

In Perl 5[edit]

Perl is distinguished by total circularity of classes – every class is the class of itself. Therefore, every class can be regarded as a metaclass. However, an additional requirement that a metaclass cannot have terminal instances would imply that in Perl, no class should be considered a metaclass.

Python example[edit]

In Python, the builtin class type is a metaclass.[3][4][5] Consider this simple Python class:

class Car(object):
    __slots__ = ['make', 'model', 'year', 'color']
 
    def __init__(self, make, model, year, color):
        self.make = make
        self.model = model
        self.year = year
        self.color = color
 
    @property 
    def description(self):
        """ Return a description of this car. """
        return "%s %s %s %s" % (self.color, self.year, self.make, self.model)

At run time, Car itself is an instance of type. The source code of the Car class, shown above, does not include such details as the size in bytes of Car objects, their binary layout in memory, how they are allocated, that the __init__ method is automatically called each time a Car is created, and so on. These details come into play not only when a new Car object is created, but also each time any attribute of a Car is accessed. In languages without metaclasses, these details are defined by the language specification and can't be overridden. In Python, the metaclass, type, controls these details of Car's behavior. They can be overridden by using a different metaclass instead of type.

The above example contains some redundant code to do with the four attributes make, model, year, and color. It is possible to eliminate some of this redundancy using a metaclass. In Python, a metaclass is most easily defined as a subclass of type.

 class AttributeInitType(type):
     def __call__(self, *args, **kwargs):
         """ Create a new instance. """
 
         # First, create the object in the normal default way.
         obj = type.__call__(self, *args)
 
         # Additionally, set attributes on the new object.
         for name, value in kwargs.items():
             setattr(obj, name, value)
 
         # Return the new object.
         return obj

This metaclass only overrides object creation. All other aspects of class and object behavior are still handled by type.

Now the class Car can be rewritten to use this metaclass. This is done in Python 2 by assigning to __metaclass__ within the class definition (in Python 3 you provide a named argument, metaclass=M to the class definition instead):

 class Car(object):
     __metaclass__ = AttributeInitType
     __slots__ = ['make', 'model', 'year', 'color']
 
     @property
     def description(self):
         """ Return a description of this car. """
         return "%s %s %s %s" % (self.color, self.year, self.make, self.model)

Car objects can then be instantiated like this:

 cars = [
     Car(make='Toyota', model='Prius', year=2005, color='green'),
     Car(make='Ford', model='Prefect', year=1979, color='blue')]

In Smalltalk-80[edit]

The Smalltalk-80 metaclass hierarchy as a UML diagram
Diagram of the inheritance and instance relationships between classes and metaclasses in Smalltalk

In Smalltalk, everything is an object. Additionally, Smalltalk is a class based system, which means that every object has a class that defines the structure of that object (i.e. the instance variables the object has) and the messages an object understands. Together this implies that a class in Smalltalk is an object and that therefore a class needs to be an instance of a class (called metaclass).

As an example, a car object c is an instance of the class Car. In turn, the class Car is again an object and as such an instance of the metaclass of Car called Car class. Note the blank in the name of the metaclass. The name of the metaclass is the Smalltalk expression that, when evaluated, results in the metaclass object. Thus evaluating Car class results in the metaclass object for Car whose name is Car class (one can confirm this by evaluating Car class name which returns the name of the metaclass of Car.)

Class methods actually belong to the metaclass, just as instance methods actually belong to the class. When a message is sent to the object 2, the search for the method starts in Integer. If it is not found it proceeds up the superclass chain, stopping at Object whether it is found or not.

When a message is sent to Integer the search for the method starts in Integer class and proceeds up the superclass chain to Object class. Note that, so far, the metaclass inheritance chain exactly follows that of the class inheritance chain. But the metaclass chain extends further because Object class is the subclass of Class. All metaclasses are subclasses of Class.

In early Smalltalks, there was only one metaclass called Class. This implied that the methods all classes have were the same, in particular the method to create new objects, i.e., new. To allow classes to have their own methods and their own instance variables (called class instance variables and should not be confused with class variables), Smalltalk-80 introduced for each class C their own metaclass C class. This means that each metaclass is effectively a singleton class.

Since there is no requirement that metaclasses behave differently from each other, all metaclasses are instances of only one class called Metaclass. The metaclass of Metaclass is called Metaclass class which again is an instance of class Metaclass.

In Smalltalk-80, every class (except Object) has a superclass. The abstract superclass of all metaclasses is Class, which describes the general nature of classes.

The superclass hierarchy for metaclasses parallels that for classes, except for class Object. ALL metaclasses are subclasses of Class, therefore:

  • Object class superclass == Class.

Like conjoined twins, classes and metaclasses are born together. Metaclass has an instance variable thisClass, which points to its conjoined class. Note that the usual Smalltalk class browser does not show metaclasses as separate classes. Instead the class browser allows to edit the class together with its metaclass at the same time.

The names of classes in the metaclass hierarchy are easily confused with the concepts of the same name. For instance:

  • Object is the base class that provides common methods for all objects; "an object" is an integer, or a widget, or a Car, etc.
  • Class is the base of the metaclasses that provides common methods for all classes (though it is not a metaclass itself); "a class" is something like Integer, or Widget, or Car, etc.
  • Metaclass provides common methods for all metaclasses.

Four classes provide the facilities to describe new classes. Their inheritance hierarchy (from Object), and the main facilities they provide are:

Object - default behavior common to all objects, like class access
Behavior - minimum state for compiling methods and creating/running objects
ClassDescription (abstract class) - class/variable naming, comments
Class - similar, more comprehensive, facilities to superclasses
Metaclass - initializing class variables, instance creation messages

In Ruby[edit]

Ruby purifies the Smalltalk-80 concept of metaclasses by introducing eigenclasses and (un)redefining the class-of map. In particular, there is no Metaclass class in Ruby and the class of every non-terminal object is constantly the Class class.

The change can be schematized as follows (note that we are using terminology in which classes and metaclasses form disjoint sets)

[6]
Smalltalk-80 Ruby
Classes
Metaclasses
Terminal
objects

Classes
Eigenclasses of
classes
Terminal
objects
Eigenclasses of
terminal objects

The Smalltalk-80 class-of map is treated as a hidden "actualclass" map [7] and is provided in its decomposition into the eigenclass-of map and the (real) class-of map (which are available via the singleton_class and the class methods, respectively). The maps can be briefly described as follows:

  • The eigenclass of a terminal object x provides "singleton methods" for x and does not have any correspondent in Smalltalk-80. In most cases, there is no need to define singleton methods, so that the eigenclass of x exists only conceptually and is not allocated.
  • The eigenclass of a class x is the metaclass for x in the Smalltalk-80 sense (but also might exist only conceptually).
  • The class of a terminal object x is equal to "the-class-of" x in the Smalltalk-80 sense.
  • The class of anything else (i.e. a class or an eigenclass) is the Class class.

In this description, the eigenclass map is presented in its restriction to primary objects (classes and terminal objects). But the concept of eigenclasses is uniform: there are also eigenclasses of eigenclasses, their eigenclasses, and so on, constituting infinite chains, thus establishing infinite regress. The "higher order" eigenclasses usually exist purely conceptually – they do not contain any methods or store any (other) data in most Ruby programs.

The decomposition purifies the actualclass map in the sense that the eigenclass map is one-to-one and the (real) class map is many-to-one, without any inherent one-to-one part.

In accordance to the alternative definition, Ruby contains exactly one explicit metaclass: the Class class which is the metaclass root. Creation of subclasses of this class is disallowed. Implicit metaclasses are then all eigenclasses that are descendants of the metaclass root, in particular, eigenclasses of classes.

In Objective-C[edit]

Diagram of the inheritance and instance relationships between classes and metaclasses in Objective-C. Note that Objective-C has multiple root classes; each root class would have a separate hierarchy. This diagram only shows the hierarchy for an example root class NSObject. Each other root class would have a similar hierarchy.

Metaclasses in Objective-C are almost the same as those in Smalltalk-80—not surprising since Objective-C borrows a lot from Smalltalk. Like Smalltalk, in Objective-C, the instance variables and methods are defined by an object's class. A class is an object, hence it is an instance of a metaclass.

Like Smalltalk, in Objective-C, class methods are simply methods called on the class object, hence a class's class methods must be defined as instance methods in its metaclass. Because different classes can have different sets of class methods, each class must have its own separate metaclass. Classes and metaclasses are always created as a pair: the runtime has functions objc_allocateClassPair() and objc_registerClassPair() to create and register class-metaclass pairs, respectively.

There are no names for the metaclasses; however, a pointer to any class object can be referred to with the generic type Class (similar to the type id being used for a pointer to any object).

Because class methods are inherited through inheritance, like Smalltalk, metaclasses must follow an inheritance scheme paralleling that of classes (e.g. if class A's parent class is class B, then A's metaclass's parent class is B's metaclass), except that of the root class.

Unlike Smalltalk, the metaclass of the root class inherits from the root class (usually NSObject using the Cocoa framework) itself. This ensures that all class objects are ultimately instances of the root class, so that you can use the instance methods of the root class, usually useful utility methods for objects, on class objects themselves.

Since metaclass objects do not behave differently (you cannot add class methods for a metaclass, so metaclass objects all have the same methods), they are all instances of the same class—the metaclass of the root class (unlike Smalltalk). Thus, the metaclass of the root class is an instance of itself. The reason for this is that all metaclasses inherit from root class; hence, they must inherit the class methods of the root class.[8]

Support in languages and tools[edit]

The following are some of the most prominent programming languages that support metaclasses.

Some less widespread languages that support metaclasses include OpenJava, OpenC++, OpenAda, CorbaScript, ObjVLisp, Object-Z, MODEL-K, XOTcl, and MELDC. Several of these languages date from the early 1990s and are of academic interest.[9]

Logtalk, an object-oriented extension of Prolog, also supports metaclasses.

Resource Description Framework (RDF) and Unified Modeling Language (UML) both support metaclasses.

See also[edit]

Notes[edit]

  1. ^ This is e.g. the case of the instanceof operator in Java or the isinstance() function in Python.

References[edit]

  1. ^ Ira R. Forman and Scott Danforth (1999). Putting Metaclasses to Work. ISBN 0-201-43305-2. 
  2. ^ "Object Membership: The core structure of object-oriented programming". 
  3. ^ IBM Metaclass programming in Python, parts 1, 2 and 3
  4. ^ Artima Forum: Metaclasses in Python 3.0 (part 1 of 2) (part 2 of 2)
  5. ^ David Mertz. "A Primer on Python Metaclass Programming". ONLamp. Retrieved June 28, 2006. 
  6. ^ "The Ruby Object Model: Comparison with Smalltalk-80". 
  7. ^ "The Ruby Object Model: Data structure in detail". 
  8. ^ http://cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html Cocoa with Love: What is a meta-class in Objective-C?
  9. ^ An implementation of mixins in Java using metaclasses