Metaclass

In object-oriented programming, a metaclass is a class whose instances are classes themselves. Unlike ordinary classes, which define the behaviors of objects, metaclasses specify the behaviors of classes and their instances. Not all object-oriented programming languages support the concept of metaclasses. For those that do, the extent of control metaclasses have over class behaviors varies. Metaclasses are often implemented by treating classes as first-class citizens, making a metaclass an object that creates and manages these classes. Each programming language adheres to its own metaobject protocol, which are the rules that determine interactions among objects, classes, and metaclasses. Metaclasses are utilized to automate code generation and to enhance framework development.

Python example
In Python, the builtin class  is a metaclass. Consider this simple Python class:

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

The above example contains some redundant code to do with the four attributes,  ,  , and. It is possible to eliminate some of this redundancy using a custom metaclass. In Python, a metaclass is most easily defined as a subclass of. This metaclass only overrides object creation. All other aspects of class and object behavior are still handled by.

Now the class  can be rewritten to use this metaclass. In Python 3 this is done by providing a "keyword argument"  to the class definition:

The resulting object  can be instantiated as usual, but can contain any number of keyword arguments:

In Smalltalk-80


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  is an instance of the class. In turn, the class  is again an object and as such an instance of the metaclass of   called. 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  results in the metaclass object for   whose name is   (one can confirm this by evaluating   which returns the name of the metaclass of  .)

Class methods actually belong to the metaclass, just as instance methods actually belong to the class. When a message is sent to the object, the search for the method starts in. 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  the search for the method starts in   and proceeds up the superclass chain to. Note that, so far, the metaclass inheritance chain exactly follows that of the class inheritance chain. But the metaclass chain extends further because  is the subclass of. All metaclasses are subclasses of Class.

In early Smalltalks, there was only one metaclass called. This implied that the methods all classes have were the same, in particular the method to create new objects, i.e.,. 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  their own metaclass. 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. The metaclass of  is called   which again is an instance of class.

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

The superclass hierarchy for metaclasses parallels that for classes, except for class. ALL metaclasses are subclasses of, therefore: Like conjoined twins, classes and metaclasses are born together. has an instance variable, 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:
 * is the base class that provides common methods for all objects; "an object" is an integer, or a widget, or a, etc.
 * 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, or  , or  , etc.
 * 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
Ruby purifies the Smalltalk-80 concept of metaclasses by introducing eigenclasses, removing the  class, and (un)redefining the class-of map.

The change can be schematized as follows: Note in particular the correspondence between Smalltalk's implicit metaclasses and Ruby's eigenclasses of classes. The Ruby eigenclass model makes the concept of implicit metaclasses fully uniform: every object x has its own meta-object, called the eigenclass of x, which is one meta-level higher than x. The "higher order" eigenclasses usually exist purely conceptually – they do not contain any methods or store any (other) data in most Ruby programs.

The following diagrams show a sample core structure of Smalltalk-80 and Ruby in comparison. In both languages, the structure consists of a built-in part which contains the circular objects (i.e. objects that appear in a cycle formed by a combination of blue or green links) and a user-part which has four explicit objects: classes  and and terminal objects  and. Green links show the child→parent relation of inheritance (with the implicit upward direction), blue links show the complementary member→container relation of instantiation (a blue link from x points to the least actual container of x that is the start point for the method lookup when a method is invoked on x). Gray nodes display the eigenclasses (resp. implicit metaclasses in the case of Smalltalk-80).

The diagram on the right also provides a picture of lazy evaluation of eigenclasses in Ruby. The  object can have its eigenclass evaluated (allocated) as a consequence of adding singleton methods to.

According to the Ruby's introspection method named , the class of every class (and of every eigenclass) is constantly the  class (denoted by   in the diagram). , and  are the only classes that have classes as instances. Subclassing of  is disallowed.

Following the standard definition of metaclasses we can conclude that  and   are the only metaclasses in Ruby. This seems to contradict the correspondence between Ruby and Smalltalk, since in Smalltalk-80, every class has its own metaclass. The discrepancy is based on the disagreement between the  introspection method in Ruby and Smalltalk. While the map x ↦ x. coincides on terminal objects, it differs in the restriction to classes. As already mentioned above, for a class, the Ruby expression   evaluates constantly to. In Smalltalk-80, if  is a class then the expression   corresponds to the Ruby's – which evaluates to the eigenclass of.

In Objective-C


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  and   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  (similar to the type   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  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.

Support in languages and tools
The following are some of the most prominent programming languages that support metaclasses.
 * Common Lisp, via CLOS
 * Delphi and other versions of Object Pascal influenced by it
 * Groovy
 * Objective-C
 * Python
 * Perl, via the metaclass pragma, as well as Moose
 * Ruby
 * Smalltalk
 * C++ (planned for C++23)

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.

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

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