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

Overload Resolution in Type Systems

Graduate Depth 94 in the knowledge graph ☐ I know this ☆ Set as goal
3topics build on this
504prerequisites beneath it
See this on the map →
Ad Hoc Polymorphism and Function OverloadingType Systems Overview→→Polymorphism and Type VariablesSubtyping and Type Bounds
type-systems polymorphism overloading

Core Idea

Overload resolution selects the best-matching function among multiple declarations with the same name. It uses specificity rules (int matches int better than Object), type compatibility (subtype matches supertype), and tie-breaking by definition order, enabling elegant APIs where operations feel unified despite different implementations.

Explainer

From your work on type systems and ad-hoc polymorphism, you know that overloading lets multiple functions share one name as long as their parameter types differ. The programmer writes `print(42)` and `print("hello")` without caring which implementation runs — but the compiler must decide. Overload resolution is the algorithm that makes that decision, and its complexity grows surprisingly fast as type systems become richer.

The resolution process begins with candidate gathering: the compiler collects every visible function declaration with the matching name. Next comes applicability filtering — each candidate is tested against the actual argument types at the call site. A candidate is applicable if every argument can be converted to the corresponding parameter type, either exactly, through implicit widening (like `int` to `long`), or through subtype relationships. If no candidate is applicable, the compiler reports an error. If exactly one survives, it wins. The interesting case is when multiple candidates remain.

When several candidates are all applicable, the compiler needs a specificity ranking to pick the best one. The core principle is that a more specific match beats a less specific one. If you call `f(5)` and both `f(int)` and `f(Object)` are candidates, `f(int)` wins because `int` is a tighter match — no conversion is needed. The ranking extends to multiple parameters: candidate A is more specific than candidate B if every parameter of A is at least as specific as the corresponding parameter of B, and at least one is strictly more specific. When neither candidate dominates the other across all parameters, the call is ambiguous, and the compiler must reject it rather than guess. Languages like Java and C# add further layers — autoboxing, varargs, and generic type argument inference each introduce additional resolution phases with carefully defined priority ordering.

The subtlety of overload resolution is that it interacts with nearly every other feature in the type system. Generic methods require the compiler to infer type arguments before comparing specificity. Implicit conversions widen the set of applicable candidates, sometimes in surprising ways. Subtype polymorphism means that a method inherited from a superclass competes with one defined in a subclass. Each language defines its own precedence rules to navigate these interactions, and getting them wrong produces confusing "ambiguous call" errors or, worse, silently selects the wrong overload. Understanding the resolution algorithm is essential for both compiler implementers who must get it right and library designers who want overloaded APIs to behave intuitively.

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 → Overload Resolution in Type Systems

Longest path: 95 steps · 504 total prerequisite topics

Prerequisites (2)

Leads To (2)