![]() |
|
|
| |
|
||||
In computer science, polymorphism is the idea of allowing the same code to be used with different types, resulting in more general and abstract implementations. The concept of polymorphism applies to functions as well as types. A function that can evaluate to and be applied to values of different types is known as a polymorphic function. A datatype that contains elements of an unspecified type is known as a polymorphic datatype. There are two fundamentally different kinds of polymorphism. If the range of actual types that can be used is finite and the combinations must be specified individually prior to use, it is called ad-hoc polymorphism. If all code is written without mention of any specific type and thus can be used transparently with any number of new types, it is called parametric polymorphism. Programming using the latter kind is called generic programming, particularly in the object-oriented community. However, in many statically typed functional programming languages the notion of parametric polymorphism is so deeply ingrained that most programmers simply take it for granted. Polymorphism gained most of its momentum when object-oriented programming became a buzzword.
Parametric polymorphismUsing parametric polymorphism, a function or datatype can be written generically so that it can deal equally well with objects of various types. For example, a function Parametric polymorphism was the first type of polymorphism developed, first identified by Christopher Strachey in 1967. It was also the first type of polymorphism to appear in an actual programming language, ML in 1976. It exists today in Standard ML, O'Caml, Haskell, and others. Some argue that templates should be considered an example of parametric polymorphism, though instead of actually reusing generic code they rely on macros to generate specific code (which can result in code bloat). Parametric polymorphism is a way to make a language more expressible, while still maintaining full static type-safety. It is thus irrelevant in dynamically typed languages, since by definition they lack static type-safety. However, any dynamically typed function f that takes n arguments can be given a static type using parametric polymorphism: f : p1 × ... × pn → r, where p1, ..., pn and r are type parameters. Of course, this type is completely insubstantial and thus essentially useless. Impredicative polymorphismThis is a more powerful form of parametric polymorphism. The most common form of this in general use today is ML-style polymorphism (also called let-polymorphism). Predicative polymorphismThis type of polymorphism is similar to the impredicative polymorphism but a little less powerful in the sense that the type applications can only take actual concrete types as arguments while in impredicative polymorphism, type application can be passed arbitrary type schemas as argument(s). Subtyping Polymorphism (Dynamic Typing)Some languages employ the idea of subtypes to restrict the range of types that can be used in a particular case of parametric polymorphism. In these languages, subtyping polymorphism (sometimes referred to as dynamic polymorphism or dynamic typing) allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T (according to the Liskov substitution principle). This type relation is sometimes written S <: T. Conversely, T is said to be a supertype of S—written T :> S. For example, if Haskell’s type system implements subtyping polymorphism using type classes. Analoguously to interfaces in Java, a type class declares a set of polymorphic operations that can be performed on any object whose type belongs to the class. This is then used to add constraints to types, limiting the set of objects that belong to them, while reciprocally expanding the set of things that can be done to the objects. For example, a list of ordered elements is typed Object-oriented programming environments such as C++ and GObject implement subtyping polymorphism using subclassing (also known as inheritance). In C++, each class contains what is called a virtual table—a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the "vtable" of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of
The same goes for most other popular object systems. Some, however, such as CLOS, provide multiple dispatch, under which method calls are polymorphic in all arguments. Ad-hoc polymorphismAd-hoc polymorphism usually refers to simple overloading (see function overloading), but sometimes automatic type conversion, known as coercion, is also considered to be a kind of ad-hoc polymorphism (see the example section below). Common to these two types is the fact that the programmer has to specify exactly what types are to be usable with the polymorphic function. The name refers to the manner in which this kind of polymorphism is typically introduced: “Oh, hey, let’s make the
OverloadingOverloading allows multiple functions taking different types to be defined with the same name; the compiler or interpreter automatically calls the right one. This way, functions appending lists of integers, lists of strings, lists of real numbers, and so on could be written, and all be called append—and the right append function would be called based on the type of lists being appended. This differs from parametric polymorphism, in which the function would need to be written generically, to work with any kind of list. Using overloading, it is possible to have a function perform two completely different things based on the type of input passed to it; this is not possible with parametric polymorphism. This type of polymorphism is common in object-oriented programming languages, many of which allow operators to be overloaded in a manner similar to functions (see operator overloading). It is also used extensively in the purely functional programming language Haskell. Many languages lacking ad-hoc polymorphism suffer from long-winded names such as An advantage that is sometimes gained from overloading is the appearance of specialization, e.g. a function with the same name can be implemented in multiple different ways, each optimized for the particular data types that it operates on. This can provide a convenient interface for code that needs to be specialized to multiple situations for performance reasons. CoercionDue to a concept known as coercion, a function can become polymorphic without being initially designed for it. Let f be a function that takes an argument of type T, and S be a type that can be automatically converted to T. Then f can be said to be polymorphic with respect to S and T. Some languages (e.g., C, Java), provide a fixed set of conversion rules, while others (e.g., C++) allow new conversion rules to be defined by the programmer. While calling C “polymorphic” is perhaps stretching it, the facility for automatic conversion (i.e., casting operators) found in C++ adds a whole new class of polymorphism to the language. ExampleThis example aims to illustrate the three different kinds of polymorphism described in this article. Though overloading an originally arithmetic operator to do a wide variety of things in this way may not be the most clear-cut example, it allows some subtle points to be made. In practice, the different types of polymorphism are not generally mixed up as much as they are here. Imagine, if you will, an operator
OverloadingTo handle these six function calls, four different pieces of code are needed—or three, if strings are considered to be lists of characters:
Thus, the name CoercionAs we’ve seen, there’s one function for adding two integers and one for adding two floating-point numbers in this hypothetical programming environment, but note that there is no function for adding an integer to a floating-point number. The reason why we can still do this is that when the compiler/interpreter finds a function call In our case, since any integer can be converted into a floating-point number without loss of precision, Parametric polymorphismFinally, the reason why we can concatenate both lists of integers, lists of booleans, and lists of characters, is that the function for list concatenation was written without any regard to the type of elements stored in the lists. This is an example of parametric polymorphism. If you wanted to, you could make up a thousand different new types of lists, and the generic list concatenation function would happily and without requiring any augmentation accept instances of them all. It can be argued, however, that this polymorphism is not really a property of the function per se; that if the function is polymorphic, it is due to the fact that the list datatype is polymorphic. This is true—to an extent, at least—but it is important to note that the function could just as well have been defined to take as a second argument an element to append to the list, instead of another list to concatenate to the first. If this was the case, the function would indisputably be parametrically polymorphic, because it could then not know anything about its second argument, except that the type of the element should match the type of the elements of the list. The notion of parametric polymorphism was first described by Christopher Strachey. System F is a lambda calculus with parametric polymorphism.
de:Polymorphie (Programmierung) et:Polümorfism (informaatika) pl:Polimorfizm (informatyka) ru:Полиморфизм he:Polymorphism |
||
|
|
|
|
|
|
Copyright 2008 WordIQ.com - Privacy Policy
::
Terms of Use
:: Contact Us
:: About Us This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Type polymorphism". |