Ad-hoc Polymorphism - Early Binding

Early Binding

Ad-hoc polymorphism is a 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 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—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.

This type of polymorphism is common in object-oriented programming languages, many of which allow operators 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 print_int, print_string, etc. This can be seen as advantage (more descriptive) or a disadvantage (overly verbose) depending on one's point of view.

An advantage that is sometimes gained from overloading is the appearance of specialization, e.g., a function with the same name can be implemented in multiple different ways, each optimized for the particular data types that it operates on. This can provide a convenient interface for code that needs to be specialized to multiple situations for performance reasons.

Since overloading is done at compile time, it is not a substitute for late binding as found in subtyping polymorphism.

Read more about this topic:  Ad-hoc Polymorphism

Famous quotes containing the words early and/or binding:

    No two men see the world exactly alike, and different temperaments will apply in different ways a principle that they both acknowledge. The same man will, indeed, often see and judge the same things differently on different occasions: early convictions must give way to more mature ones. Nevertheless, may not the opinions that a man holds and expresses withstand all trials, if he only remains true to himself and others?
    Johann Wolfgang Von Goethe (1749–1832)

    With a binding like you’ve got, people are gonna want to know what’s in the book.
    Alan Jay Lerner (1918–1986)