Previous    Next


Some languages permit overloading: different functions of the same name but different argument types. The compiler must choose between function bodies based on the types of the actual parameters. This is sometimes known as ad hoc polymorphism, as opposed to the parametric polymorphism described in the previous sections. Static overloading is not difficult to implement. When processing the declaration of an overloaded function f, the new binding bn must not hide the old definitions b1,…, bn−1. Instead, the new binding maps f to a list of different implementations, f ↦ [b1,…, bn]. Depending on the language semantics, it may be necessary to give an error message if bn has identical parameter types to one of the bi. Then, when looking up f in a place where it is called with actual parameters, the types of the actual parameters will determine which of the bindings bi should be used.

Some languages allow functions of identical argument types (but different result type) to be overloaded; some languages allow forms of dynamic overloading; see the Further Reading section.

JaVaScreenshot Previous    Next