Content deleted Content added
adding link to references using Google Scholar |
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist. |
||
(12 intermediate revisions by 10 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.
==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
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 14 ⟶ 15:
The previous section notwithstanding, there are other ways in which ''ad hoc'' polymorphism can work out. Consider for example the Smalltalk language. In [[Smalltalk]], the overloading is done at run time, as the methods ("function implementation") for each overloaded message ("overloaded function") are resolved when they are about to be executed. This happens at run time, after the program is compiled. Therefore, polymorphism is given by [[subtyping polymorphism]] as in other languages, and it is also extended in functionality by ''ad hoc'' polymorphism at run time.
A closer look will also reveal that Smalltalk provides a slightly different variety of ''ad hoc'' polymorphism. Since Smalltalk has a late bound execution model, and since it provides objects the ability to handle messages that are not understood, it is possible to
Also, while in general terms common class method and constructor overloading is not considered polymorphism, there are more uniform languages in which classes are regular objects. In Smalltalk, for instance, classes are regular objects. In turn, this means messages sent to classes can be overloaded, and it is also possible to create objects that behave like classes without their classes inheriting from the hierarchy of classes. These are effective techniques
==Example==
Line 26 ⟶ 27:
# <code>[true, false] + [false, true] = [true, false, false, true]</code>
# <code>"bab" + "oon" = "baboon"</code>
To handle these six function calls, four different pieces of code are needed
▲To handle these six function calls, four different pieces of code are needed—or ''three'', if strings are considered to be lists of characters:
* In the first case, [[integer (computer science)|integer]] addition must be invoked.
* In the second and third cases, [[floating point|floating-point]] addition must be invoked (with [[type promotion]], or [[type coercion]], in the third case).
* 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]]''.
== See also ==
* [[Operator overloading]]
* [[Type class]]
* [[Polymorphism (computer science)]] (other kinds of polymorphism)
* [[Parametric polymorphism]]
==References==
|