This article has multiple issues. Please help improve it or discuss these issues on the talk page . (Learn how and when to remove these messages)
|
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. [1] Metaclasses are utilized to automate code generation and to enhance framework development. [2]
In Python, the builtin class type
is a metaclass. [3] [4] [5] Consider this simple Python class:
classCar:def__init__(self,make:str,model:str,year:int,color:str):self.make=makeself.model=modelself.year=yearself.color=color@propertydefdescription(self)->str:"""Return a description of this car."""returnf"{self.color}{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 custom metaclass. In Python, a metaclass is most easily defined as a subclass of type
.
classAttributeInitType(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.forname,valueinkwargs.items():setattr(obj,name,value)# Return the new object.returnobj
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. In Python 3 this is done by providing a "keyword argument" metaclass
to the class definition:
classCar(object,metaclass=AttributeInitType):@propertydefdescription(self)->str:"""Return a description of this car."""return" ".join(str(value)forvalueinself.__dict__.values())
The resulting object Car
can be instantiated as usual, but can contain any number of keyword arguments:
new_car=Car(make='Toyota',model='Prius',year=2005,color='Green',engine='Hybrid')
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:
Ruby purifies the Smalltalk-80 concept of metaclasses by introducing eigenclasses, removing the Metaclass
class, and (un)redefining the class-of map. The change can be schematized as follows: [6]
| → |
|
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. [7]
The following diagrams show a sample core structure of Smalltalk-80 and Ruby in comparison. [8] 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 A
and B
and terminal objects u
and v
. 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).
Smalltalk-80 | Ruby | |
The diagram on the right also provides a picture of lazy evaluation of eigenclasses in Ruby. The v
object can have its eigenclass evaluated (allocated) as a consequence of adding singleton methods to v
.
According to the Ruby's introspection method named class
, the class of every class (and of every eigenclass) is constantly the Class
class (denoted by c
in the diagram). Class
, and Struct
are the only classes that have classes as instances. [9] [ disputed – discuss ] Subclassing of Class
is disallowed. Following the standard definition of metaclasses we can conclude that Class
and Struct
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 class
introspection method in Ruby and Smalltalk. While the map x ↦ x.class
coincides on terminal objects, it differs in the restriction to classes. As already mentioned above, for a class x
, the Ruby expression x.class
evaluates constantly to Class
. In Smalltalk-80, if x
is a class then the expression x class
corresponds to the Ruby's x.singleton_class
– which evaluates to the eigenclass of x
.
This section needs additional citations for verification .(October 2013) |
This section's tone or style may not reflect the encyclopedic tone used on Wikipedia.(September 2013) |
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. [10]
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. [12]
Logtalk, an object-oriented extension of Prolog, also supports metaclasses.
Resource Description Framework (RDF) and Unified Modeling Language (UML) both support metaclasses.
In object-oriented programming, a class defines the shared aspects of objects created from the class. The capabilities of a class differ between programming languages, but generally the shared aspects consist of state (variables) and behavior (methods) that are each either associated with a particular object or with all objects of that class.
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.
Ruby is an interpreted, high-level, general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including primitive data types. It was developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learning Research Group (LRG) scientists, including Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, and Scott Wallace.
The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java. CLOS was inspired by earlier Lisp object systems such as MIT Flavors and CommonLoops, although it is more general than either. Originally proposed as an add-on, CLOS was adopted as part of the ANSI standard for Common Lisp and has been adapted into other Lisp dialects such as EuLisp or Emacs Lisp.
In object-oriented (OO) and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.
In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.
In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability.
In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.
In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. In addition to providing data-driven algorithm-determined parameters across virtual network interfaces, it also allows for a specific type of polymorphism (subtyping). The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. This helps in preventing problems associated with differential relay analytics which would otherwise rely on a framework in which method overriding might be obviated. Some languages allow a programmer to prevent a method from being overridden.
ObjVlisp is a 1984 object-oriented extension of Vlisp–Vincennes LISP, a LISP dialect developed since 1971 at the University of Paris VIII – Vincennes. It is noteworthy as one of the earliest implementations of the concept of metaclasses, and in particular explicit metaclasses. In the ObjVlisp model, "each entity is an instance of a single class. Classes are instances of other classes, called metaclasses. This model allows for extension of the static part of OOL, i.e. the structural aspects of objects considered as implementation of abstract data types"
The uniform access principle of computer programming was put forth by Bertrand Meyer. It states "All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation." This principle applies generally to the syntax of object-oriented programming languages. In simpler form, it states that there should be no syntactical difference between working with an attribute, pre-computed property, or method/query of an object.
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes. In most class-based object-oriented languages like C++, an object created through inheritance, a "child object", acquires all the properties and behaviors of the "parent object", with the exception of: constructors, destructors, overloaded operators and friend functions of the base class. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors, to reuse code and to independently extend original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a directed acyclic graph.
PyObjC is a bidirectional bridge between the Python and Objective-C programming languages, allowing programmers to use and extend existing Objective-C libraries, such as Apple's Cocoa framework, using Python.
this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity which the currently running code is a part of. The entity referred to thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, these keywords can disambiguate variables and functions with the same name.
This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.
Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields, and code in the form of procedures. In OOP, computer programs are designed by making them out of objects that interact with one another.
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard language used, supported, and promoted by Apple for developing macOS and iOS applications from 1997, when Apple purchased NeXT until the introduction of the Swift language in 2014.
In object-oriented programming, forwarding means that using a member of an object results in actually using the corresponding member of a different object: the use is forwarded to another object. Forwarding is used in a number of design patterns, where some members are forwarded to another object, while others are handled by the directly used object. The forwarding object is frequently called a wrapper object, and explicit forwarding members are called wrapper functions.