Content deleted Content added
m →top: Spelling/grammar/punctuation/typographical correction |
→Cite book, tweak cites |
||
Line 2:
{{Polymorphism}}
In [[programming language]]s and [[type theory]], '''polymorphism''' is the provision of a single [[interface (computing)|interface]] to entities of different [[Data type|type]]s<ref>
{{cite web | url=http://www.stroustrup.com/glossary.html#Gpolymorphism | author=Bjarne Stroustrup | title=Bjarne Stroustrup's C++ Glossary | date=February 19, 2007 | quote=polymorphism – providing a single interface to entities of different types.}}</ref> or the use of a single symbol to represent multiple different types.<ref name="Luca">{{Cite journal | last1 = Cardelli | first1 = Luca| author-link1 = Luca Cardelli| last2 = Wegner | first2 = Peter| author-link2 = Peter Wegner| doi = 10.1145/6041.6042| title = On understanding types, data abstraction, and polymorphism| journal = [[ACM Computing Surveys]]
The most commonly recognized major classes of polymorphism are:
* ''[[Ad hoc polymorphism]]'': defines a common interface for an arbitrary set of individually specified types.
* ''[[Parametric polymorphism]]'': when one or more types are not specified by name but by abstract symbols that can represent any type.
* ''[[Subtyping]]'' (also called ''subtype polymorphism'' or ''inclusion polymorphism''): when a name denotes instances of many different classes related by some common superclass.<ref name="gbooch">{{cite book |last=Conallen |first=J. |last2=Engle |first2=M. |last3=Houston |first3=K. |last4=Maksimchuk |first4=R. |last5=Young |first5=B. |last6=Booch
==History==
Interest in polymorphic [[type system]]s developed significantly in the 1960s, with practical implementations beginning to appear by the end of the decade. ''Ad hoc polymorphism'' and ''parametric polymorphism'' were originally described in [[Christopher Strachey]]'s ''[[Fundamental Concepts in Programming Languages]]'',<ref name=Strachey00>{{cite journal |last1=Strachey |first1=Christopher |title=Fundamental Concepts in Programming Languages |journal=[[Higher-Order and Symbolic Computation]] |date=2000 |volume=13 |issue=1/2 |pages=11–49 |doi=10.1023/A:1010000313106 |issn=1573-0557|citeseerx=10.1.1.332.3161 }}</ref> where they are listed as "the two main classes" of polymorphism. Ad hoc polymorphism was a feature of [[Algol 68]], while parametric polymorphism was the core feature of [[ML (programming language)|ML]]'s type system.
In a 1985 paper, [[Peter Wegner]] and [[Luca Cardelli]] introduced the term ''inclusion polymorphism'' to model subtypes and [[Inheritance (object-oriented programming)|inheritance]],<ref name="Luca"/> citing [[Simula]] as the first programming language to implement it.
Line 17:
===Ad hoc polymorphism===
{{main|Ad hoc polymorphism}}
[[Christopher Strachey]] chose the term ''ad hoc polymorphism'' to refer to polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known as [[function overloading]] or [[operator overloading]]).<ref name=
<syntaxhighlight lang="Pascal">
Line 40:
In [[dynamically typed]] languages the situation can be more complex as the correct function that needs to be invoked might only be determinable at run time.
[[Implicit type conversion]] has also been defined as a form of polymorphism, referred to as "coercion polymorphism".<ref name="Luca"/><ref name="Tucker2004">{{cite book |
===Parametric polymorphism===
{{main|Parametric polymorphism}}
''Parametric polymorphism'' allows a function or a data type to be written generically, so that it can handle values ''uniformly'' without depending on their type.<ref name="bjpierce">
The concept of parametric polymorphism applies to both [[data type]]s and [[function (programming)|function]]s. A function that can evaluate to or be applied to values of different types is known as a ''polymorphic function.'' A data type that can appear to be of a generalized type (e.g. a [[list (computing)|list]] with elements of arbitrary type) is designated ''polymorphic data type'' like the generalized type from which such specializations are made.
Line 142:
===Polytypism===
{{main|Generic programming#Functional languages}}
A related concept is ''polytypism'' (or ''data type genericity''). A polytypic function is more general than polymorphic, and in such a function, "though one can provide fixed ad hoc cases for specific data types, an ad hoc combinator is absent".<ref>
==Implementation aspects==
|