Ad hoc polymorphism: Difference between revisions

Content deleted Content added
Ruud Koot (talk | contribs)
clarifying lead
Line 1:
In [[programming languages]], '''ad-hoc polymorphism'''<ref>C. Strachey, Fundamental concepts in programming languages. Lecture notes for International Summer School in Computer Programming, Copenhagen, August 1967</ref> tois refera tokind polymorphicof functions[[polymorphism (computer science)|polymorphism]] in which polymorphic functions can be applied to arguments of different types, butbecause whicha polymorphic function can denote a number of distinct and potentially behaveheterogeneous differentlyimplementations depending on the type of the argument(s) to which theyit areis applied. 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==
{{Disputed-section|date=March 2008}}
 
 
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 calls the right one. This way, functions appending lists of integers, lists of strings, lists of real numbers, and so on could be written, and all be called ''append''&mdash;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. 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.