A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Ad Hoc Polymorphism and Function Overloading

Graduate Depth 93 in the knowledge graph ☐ I know this ☆ Set as goal
4topics build on this
503prerequisites beneath it
See this on the map →
Symbol Tables and Scope ResolutionType Systems Overview→→Overload Resolution in Type Systems
polymorphism type-systems overloading

Core Idea

Ad hoc polymorphism (overloading) allows functions with the same name to behave differently for different argument types. Unlike parametric polymorphism which uses a single implementation, ad hoc polymorphism provides distinct code per type, selected at compile-time during overload resolution.

Explainer

From your study of type systems, you know that types classify values and constrain the operations that can be performed on them. But consider the `+` operator: it adds integers, concatenates strings, and combines floating-point numbers. These are fundamentally different operations — integer addition uses an ALU instruction, float addition uses an FPU instruction, and string concatenation allocates memory and copies bytes. Yet the programmer writes the same symbol for all three. This is ad hoc polymorphism: the same name dispatches to entirely different implementations depending on the types of its arguments.

The term "ad hoc" contrasts with parametric polymorphism, where a single function works uniformly over all types. A parametric function like `identity(x) = x` does the same thing regardless of whether x is an integer, a string, or a list — it never inspects the type. Ad hoc polymorphism is the opposite: each type gets its own bespoke implementation. The compiler must determine *which* implementation to call, and it does so by examining the types of the arguments at the call site. This process is called overload resolution.

Overload resolution is where your knowledge of symbol tables and scope becomes critical. When the compiler encounters a call like `add(a, b)`, it looks up `add` in the symbol table and finds multiple entries — one for integers, one for floats, perhaps one for complex numbers. It then examines the types of `a` and `b` to select the best match. The rules governing this selection vary by language and can become surprisingly complex. C++ considers implicit conversions (int to float, for example), creating a ranking of matches from exact to promotion to conversion. If no single overload is strictly better than all others, the call is ambiguous and the compiler reports an error. Languages like Haskell take a different approach with type classes: a type class like `Num` declares an interface (including `+`), and each type provides its own instance, making overload resolution a matter of instance lookup rather than argument-type matching.

The compiler's implementation of ad hoc polymorphism is conceptually a dispatch table keyed by type signatures. At compile time, once overload resolution identifies the correct implementation, the call is bound to a specific function — there is no runtime overhead. This is in contrast to dynamic dispatch (as in virtual methods), where the target is resolved at runtime via a vtable. The distinction matters for performance and for the compiler's ability to inline and optimize: a statically resolved overloaded call is just a normal function call by the time code generation begins.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Type Systems Overview → Ad Hoc Polymorphism and Function Overloading

Longest path: 94 steps · 503 total prerequisite topics

Prerequisites (2)

Leads To (1)