Lesson 2 of 9 · 25m
Multiple Dispatch Is the Core Design, Not a Syntax Feature
Learn how Julia chooses methods from all argument types, why that makes scientific abstractions composable, and how to avoid ambiguous or overly narrow methods.
The first Julia feature that changes how you design software is not the JIT compiler. It is multiple dispatch. In most object-oriented languages, the receiver object owns the method choice: x.step(y) dispatches primarily on x. In Julia, the function is the abstraction and the method is chosen from the types of all arguments: step(x, y) can specialize on both.
Read dispatch as a table of behavior
abstract type Integrator end
struct Euler <: Integrator end
struct RK4 <: Integrator end
step(::Euler, f, y, dt) = y + dt * f(y)
step(::RK4, f, y, dt) = begin
k1 = f(y)
k2 = f(y + dt * k1 / 2)
k3 = f(y + dt * k2 / 2)
k4 = f(y + dt * k3)
y + dt * (k1 + 2k2 + 2k3 + k4) / 6
endThe integrator, state type, and derivative function all participate. A state can be a vector, a GPU array, a measurement with units, or a custom lattice. If it supports the arithmetic the method uses, the generic algorithm often works without rewriting.
This is why packages compose
Scientific ecosystems usually fail composition because arrays, units, automatic differentiation, and solvers each know nothing about one another. Julia's type system makes those concerns orthogonal. An ODE solver can call user arithmetic without caring whether numbers are Float64, ForwardDiff.Dual, or unit-bearing quantities. A function such as energy(m, v) = m * v^2 / 2 does not import Unitful; it simply writes correct algebra, while the package supplies types and arithmetic methods. That composability is the ecosystem's real superpower.
Dispatch is specificity, not if statements
Write methods at the level where behavior truly differs:
struct Rectangle; width; height; end
struct Circle; radius; end
area(r::Rectangle) = r.width * r.height
area(c::Circle) = pi * c.radius^2Avoid fake generic methods that inspect types internally. If you write if x isa Rectangle, you have moved dispatch back into an unextensible branch. A new type cannot add a method to your if statement.
Ambiguity is a design smell
Overlapping methods can make dispatch ambiguous:
combine(x::Integer, y) = :int_left
combine(x, y::Integer) = :int_right
combine(1, 2)Julia will report an ambiguity rather than guess. Fix it with a more specific method or rethink the algebra. Silent precedence rules would be worse because numeric code cannot afford accidental behavior.
Type parameters express families
norm2(x::AbstractVector{T}) where {T<:Real} = sqrt(sum(abs2, x)) works for Vector{Float32}, Vector{Float64}, and many vector-like containers. Constrain behavior where mathematics requires it, not where implementation happens to be today.
Field notes
- Use
methods(f)to inspect how a generic function is actually implemented. MethodErrorusually means no method exists for the combination of argument types, not that the function name is missing.- Do not over-constrain arguments to
Vector{Float64}whenAbstractVector{<:Real}is mathematically enough. - Dispatch ambiguity that appears only after loading another package means your method table crossed package boundaries; coordinate ownership carefully.
Was this lesson helpful?