Polymorphism (computer science): Difference between revisions

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]]| issn = 0360-0300| volume = 17| issue = 4| pages = 471–523| date=December 1985 | url = http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf| citeseerx = 10.1.1.117.695}}: "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) | websitepublisher=Oracle.com | 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 |last=Conallen |first=J. |last2=Engle |first2=M. |last3=Houston |first3=K. |last4=Maksimchuk |first4=R. |last5=Young |first5=B. |last6=Booch, et|first6=G. al|author6-link=Grady 2007Booch ''|title=Object-Oriented Analysis and Design with Applications.'' Addison-Wesley.|publisher=Pearson Education |edition=3rd |date=2007 |isbn=9780132797443 |pages= }}</ref>
 
==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="Strachey">{{cite book |author=Christopher Strachey |title=Fundamental Concepts in Programming Languages |url=http:Strachey00//www.itu.dk/courses/BPRD/E2009/fundamental-1967.pdf |website=www.itu.dk |publisher=Kluwer Academic Publishers |access-date=2012-10-13 |archive-url=https://web.archive.org/web/20170812012310/http://www.itu.dk/courses/BPRD/E2009/fundamental-1967.pdf |archive-date=2017-08-12 |url-status=dead }}</ref> The term "[[ad hoc]]" in this context is not intended to be pejorative; it refers simply to the fact that this type of polymorphism is not a fundamental feature of the type system. In the [[Pascal (programming language)|Pascal]] / [[Delphi (programming language)|Delphi]] example below, the <code>Add</code> functions seem to work generically over various types when looking at the invocations, but are considered to be two entirely distinct functions by the compiler for all intents and purposes:
 
<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 |authorfirst=Allen B. |last=Tucker|title=Computer Science Handbook, Second|edition=2nd Edition|url=https://books.google.com/books?id=9IFMCsQJyscC&pg=SA91-PA5|date=28 June 2004|publisher=Taylor & Francis|isbn=978-1-58488-360-9|pages=91–}}</ref>
 
===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">Pierce,{{cite book |first=B. C. 2002|last=Pierce |chapter=23.2 Varieties of Polymorphism |chapter-url=https://books.google.com.au/books?id=ti6zoAC9Ph8C&pg=PA340 ''|title=Types and Programming Languages.'' |publisher=MIT Press. |date=2002 |isbn= 9780262162098 |pages=340–1 |url=}}</ref> Parametric polymorphism is a way to make a language more expressive while still maintaining full static [[type-safety]].
 
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>Ralf{{cite Lammelbook |first=Ralf and|last=Lämmel |first2=Joost |last2=Visser, "|chapter=Typed Combinators for Generic Traversal", in ''|title=Practical Aspects of Declarative Languages: 4th International Symposium'' (|publisher=Springer |date=2002) |isbn=354043092X |pages=137–154, See p. 153 |citeseerx=10.1.1.18.5727 |url=}}</ref>
 
==Implementation aspects==