Overload Resolution in Type Systems

Graduate Depth 68 in the knowledge graph I know this Set as goal
Unlocks 3 downstream topics
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

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsInteger Order of OperationsVariable ExpressionsCombining Like TermsOne-Step EquationsTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionTime and Space ComplexityAmortized AnalysisHash TablesSymbol Tables and Scope ResolutionSemantic Analysis PhaseType Systems OverviewAd Hoc Polymorphism and Function OverloadingOverload Resolution in Type Systems

Longest path: 69 steps · 372 total prerequisite topics

Prerequisites (2)

Leads To (2)