ArticleslgStudy

computer science

Ad hoc polymorphism

Ad hoc polymorphism is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Ad hoc polymorphism rather than just read about it. In short: In programming languages, ad hoc polymorphism is a kind of 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. When applied to object-oriented or procedural concepts, it is also known as function overloading o…

Key takeaways

  • Ad hoc polymorphism belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Ad hoc polymorphism to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Ad hoc polymorphism from memory before moving on to harder problems.

Reference excerpt

In programming languages, ad hoc polymorphism is a kind of 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. When applied to object-oriented or procedural concepts, 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 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 ensures that the right function is called. 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 (but would have to be achieved with switching on the type inside the generic function). 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. The downside is that the type system cannot guarantee the consistancy of the different implementations. Since overloading is done at compile time, it is not a substitute for late binding as found in subtyping polymorphism.

Late binding 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 implement functionality using polymorphism without explicitly overloading a particular message. This may not be generally recommended practice for everyday programming, but it can be quite useful when implementing proxies. 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 that can be used to take advantage of Smalltalk's powerful reflection capabilities. Similar arrangements are also possible in languages such as Self and Newspeak.

Example Imagine an operator + that may be used in the following ways:

1 + 2 = 3 3.14 + 0.0015 = 3.1415 1 + 3.7 = 4.7 [1, 2, 3] + [4, 5, 6] = [1, 2, 3, 4, 5, 6] [true, false] + [false, true] = [true, false, false, true] "bab" + "oon" = "baboon" 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 addition must be invoked. In the second and third cases, floating-point addition must be invoked (with type promotion, or type coercion, in the third case). In the fourth and fifth cases, list concatenation must be invoked. In the last case, string concatenation must be invoked. Thus, the name + actually refers to three or four completely different functions. This is an example of overloading or more specifically, operator overloading. Note the ambiguity in the string types used in the last case. Consider "123" + "456" in which the programmer might naturally assume addition rather than concatenation. They may expect "579" instead of "123456". Overloading can therefore provide different meaning, or semantics, for an operation, as well as differing implementations.

See also Operator overloading Type class Polymorphism (computer science) (other kinds of polymorphism) Parametric polymorphism

References

Worked examples

Example 1 — a first encounter with Ad hoc polymorphism

Start with the simplest possible case. Write down what Ad hoc polymorphism claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Ad hoc polymorphism before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Ad hoc polymorphism ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Ad hoc polymorphism

In research
Ad hoc polymorphism appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Ad hoc polymorphism in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Ad hoc polymorphism is common in secondary-school and first-year university syllabi. It links to neighbouring topics Polymorphism (computer science), Programming language topics, Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Ad hoc polymorphism outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Ad hoc polymorphism” →

How to study Ad hoc polymorphism in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Ad hoc polymorphism means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Ad hoc polymorphism out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Ad hoc polymorphism in simple terms?

In programming languages, ad hoc polymorphism is a kind of 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…

Why does Ad hoc polymorphism matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Ad hoc polymorphism?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Ad hoc polymorphism.

Tags

  • Polymorphism (computer science)
  • Programming language topics
  • Type theory

Keep exploring