Polymorphism (computer science): Difference between revisions

Content deleted Content added
Ad hoc polymorphism: Improved a sentence to be understood easier.
Subtyping: Mentioned a relation between subtypes and a supertype in an example of them.
Line 4:
 
In [[programming language theory]] 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:
Line 118:
[[File:UML class pet.svg]]
 
In another example, if ''Number'', ''Rational'', and ''Integer'' are types such that ''Number''&nbsp;:&gt;&nbsp;''Rational'' and ''Number''&nbsp;:&gt;&nbsp;''Integer'' (''Rational'' and ''Integer'' as subtypes of a type ''Number'' that is a supertype of them), a function written to take a ''Number'' will work equally well when passed an ''Integer'' or ''Rational'' as when passed a ''Number''. The actual type of the object can be hidden from clients into a [[Black box (systems)|black box]], and accessed via object [[identity (object-oriented programming)|identity]]. In fact, if the ''Number'' type is ''abstract'', it may not even be possible to get your hands on an object whose ''most-derived'' type is ''Number'' (see [[abstract data type]], [[abstract class]]). This particular kind of type hierarchy is known—especially in the context of the [[Scheme (programming language)|Scheme programming language]]—as a ''[[numerical tower]]'', and usually contains many more types.
In fact, if the ''Number'' type is ''abstract'', it may not even be possible to get your hands on an object whose ''most-derived'' type is ''Number'' (see [[abstract data type]], [[abstract class]]). This particular kind of type hierarchy is known—especially in the context of the [[Scheme (programming language)|Scheme programming language]]—as a ''[[numerical tower]]'', and usually contains many more types.
 
[[Object-oriented programming language]]s offer subtype polymorphism using ''[[Subclass (computer science)|subclass]]ing'' (also known as ''[[inheritance in object-oriented programming|inheritance]]''). In typical implementations, each class contains what is called a ''[[virtual table]]''—a (shortly called ''vtable'') — 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:
* ''[[late binding]]'', because virtual function calls are not bound until the time of invocation;
* ''[[single dispatch]]'' (i.e., single-argument polymorphism), because virtual function calls are bound simply by looking through the vtable provided by the first argument (the <code>this</code> object), so the runtime types of the other arguments are completely irrelevant.
The same goes for most other popular object systems. Some, however, such as [[Common Lisp Object System]], provide ''[[multiple dispatch]]'', under which method calls are polymorphic in ''all'' arguments.
 
Line 142 ⟶ 141:
| doi = 10.1109/LICS.1989.39162
}}
</ref> is a similar, but distinct concept from subtyping. It deals with [[Structural type system|structural types]]. It allows the usage of all values whose types have certain properties, without losing the remaining type information.
 
===Polytypism===
Line 149 ⟶ 148:
 
===Rank polymorphism===
Rank polymorphism is one of the defining features of the [[Array_programming|array programming languages]], like [[APL_(programming_language)|APL]]. The essence of the rank-polymorphic programming model is implicitly treating all operations as aggregate operations, usable on arrays with arbitrarily many dimensions,<ref>{{cite arXiv|title=The semantics of rank polymorphism |date=2019 |last1=Slepak |first1=Justin |last2=Shivers |first2=Olin |last3=Manolios |first3=Panagiotis|class=cs.PL |eprint=1907.00509 }}</ref> which is to say that rank polymorphism allows functions to be defined to operate on arrays of any shape and size.
 
==Implementation aspects==