The simplest and most intuitive example is polymorphism for arithmetic operators. For example, + can be used as following:
Polymorphism has gained momentum when object-oriented programming has become popular.
As simple, a function with polymorphism is polymorphic function and a datatype with polymorphism is polymorphic type.
Table of contents |
2 Parametric polymorphism 3 Ad-hoc polymorphism 4 Subtyping polymorphism |
There are several distinct concepts, all of which are referred to as polymorphism:
In this sort of polymorphism, a function has been written generically so it can deal equally well with objects of various types. For example, an append function (one that takes two lists and appends them) can be written so it does not depend on the particular types of lists: it can append lists of integers, lists of real numbers, lists of strings, and so on. This 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, OCaml, Haskell, and others. Some also argue that templates should be considered an example of parametric polymorphism, though they rely on macros to generate specific code rather than actually reusing generic code.
Ad-hoc polymorphism is better-known as overloading. This form of polymorphism allows multiple functions with the same name, but taking different types, to be defined; 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 only one generic function needs to be written. Some argue that ad-hoc polymorphism is not polymorphism in a meaningful computer science sense at all, and is just a shorthand for the programmer calling append_integer and so on manually. This sort of polymorphism is most common in object-oriented programming languages, many of which also allow operators to be overloaded in a similar manner (see operator overloading).
Some languages employ the idea of a subtype to state what objects are permitted. Subtyping polymorphism allows a function to be written so that it takes a certain type of object, but will work correctly if passed an object that is a subtype of the object it expects. For example, if in a certain programming language with subtyping polymorphism there were a type "Number", of which "Integer" and "Real Number" were subtypes, a function could be written so that it takes a Number, and works equally well whether an Integer or Real Number is passed. In object-oriented programming this is implemented with subclassing (inheritance), and is also known as late binding. See also Polymorphism in object-oriented programming.Type polymorphism
Type polymorphism, the simplest, is allowing code to handle data objects regardless of their datatypes. This polymorphism can be done with parametric polymorphism, ad-hoc polymorphism or subtyping polymorphism.Parametric polymorphism
Ad-hoc polymorphism
Subtyping polymorphism