Content deleted Content added
→Cite book, tweak cites |
Citation bot (talk | contribs) Alter: chapter-url. URLs might have been anonymized. Add: s2cid, authors 1-1. Removed parameters. Some additions/deletions were parameter name changes. | Use this bot. Report bugs. | Suggested by AManWithNoPlan | #UCB_webform 1009/1857 |
||
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]]| volume = 17| issue = 4| pages = 471–523| date=December 1985 | url = http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf| citeseerx = 10.1.1.117.695| s2cid = 2921816}}: "Polymorphic types are types whose operations are applicable to values of more than one type."</ref>The concept is borrowed from a principle in biology where an organism or species can have many different forms or stages.<ref name="Moved">{{cite web | title=Polymorphism |work=The Java™ Tutorials: Learning the Java Language: Interfaces and Inheritance |publisher=Oracle | url=https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html | access-date=2021-09-08}}</ref>
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 |
==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 |s2cid=14124601 }}</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 44:
===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">{{cite book |first=B.C. |last=Pierce |chapter=23.2 Varieties of Polymorphism |chapter-url=https://books.google.com
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>{{cite book |
==Implementation aspects==
|