Content deleted Content added
→Example: Remove subheading that interrupts flow (revert if inappropriate) |
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist. |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 1:
{{Short description|Applying polymorphic functions to arguments of different types}}
{{Polymorphism}}
In [[programming languages]], '''ad hoc polymorphism'''<ref>C. Strachey, [http://www.ics.uci.edu/~jajones/INF102-S18/readings/05_stratchey_1967.pdf Fundamental concepts in programming languages]. Lecture notes for International Summer School in Computer Programming, Copenhagen, August 1967</ref> is a kind of [[polymorphism (computer science)|polymorphism]] in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. When applied to [[Object-oriented programming|object-oriented]] or procedural concepts, it is also known as [[function overloading]] or [[operator overloading]]. 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]]. This is in contrast to [[parametric polymorphism]], in which polymorphic functions are written without mention of any specific type, and can thus apply a single abstract implementation to any number of types in a transparent way. This classification was introduced by [[Christopher Strachey]] in 1967.
==Early binding==
Ad hoc polymorphism is a [[dynamic dispatch|dispatch]] mechanism: control moving through one named function is dispatched to various other functions without having to specify the exact function being called. Overloading allows multiple functions taking different types to be defined with the same name; the [[compiler]] or [[interpreter (computing)|interpreter]] automatically ensures that the right function is called. This way, functions appending lists of [[integer]]s, lists of [[string (computer science)|string]]s, lists of [[real number]]s, 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 the function would need to be written ''generically'', to work with any kind of list. Using overloading, it is possible to have a function perform two completely different things based on the type of input passed to it; this is not possible with parametric polymorphism (but would have to be achieved with switching on the type inside the generic function). Another way to look at overloading is that a routine is uniquely identified not by its name, but by the combination of its name and the number, order and types of its parameters.
This type of polymorphism is common in [[object-oriented programming]] languages, many of which allow [[operator (programming)|operator]]s to be overloaded in a manner similar to functions (see [[operator overloading]]). Some languages that are not dynamically typed and lack ad hoc polymorphism (including type classes) have longer function names such as <code>print_int</code>, <code>print_string</code>, etc. This can be seen as advantage (more descriptive) or a disadvantage (overly verbose) depending on one's point of view.
Line 31 ⟶ 32:
* In the fourth and fifth cases, [[List (computing)|list]] [[concatenation]] must be invoked.
* In the last case, [[literal string|string]] concatenation must be invoked.
Thus, the name <code>+</code> actually refers to three or four completely different functions. This is an example of ''[[Overloading (programming)|overloading]]'' or more specifically, ''[[operator overloading]]''.
Note the ambiguity in the string types used in the last case. Consider <code>"123" + "456"</code> in which the programmer might ''naturally'' assume addition rather than concatenation. They may expect <code>"579"</code> instead of <code>"123456"</code>. Overloading can therefore provide different meaning, or semantics, for an operation, as well as differing implementations.
== See also ==
* [[Operator overloading]]
* [[Type class]]
* [[Polymorphism (computer science)]] (other kinds of polymorphism)
* [[Parametric polymorphism]]
==References==
|