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 binding and/or early:

    What is lawful is not binding only on some and not binding on others. Lawfulness extends everywhere, through the wide-ruling air and the boundless light of the sky.
    Empedocles 484–424 B.C., Greek philosopher. The Presocratics, p. 142, ed. Philip Wheelwright, The Bobbs-Merrill Co., Inc. (1960)

    Foolish prater, What dost thou
    So early at my window do?
    Cruel bird, thou’st ta’en away
    A dream out of my arms to-day;
    A dream that ne’er must equall’d be
    By all that waking eyes may see.
    Thou this damage to repair
    Nothing half so sweet and fair,
    Nothing half so good, canst bring,
    Tho’ men say thou bring’st the Spring.
    Abraham Cowley (1618–1667)