Julia Multiple Dispatch Tutorial
last modified November 18, 2026
Multiple dispatch is Julia's defining feature. When a function is called, Julia picks the implementation to run from the types of all the arguments, not just the first one. Each implementation is called a method of the function.
This tutorial shows how methods are selected, how specificity and ambiguity
work, and how to inspect the available methods with methods,
which, and @which. The examples were verified with
Julia 1.13.4.
Dispatch on All Arguments
A generic function is a single name with several methods. The method chosen depends on the arguments of each particular call.
describe(x::Int) = "integer: $x"
describe(x::Float64) = "float: $x"
describe(x::String) = "string: $x"
println(describe(10))
println(describe(3.5))
println(describe("hi"))
We define three one-line methods of describe, each restricted to a
different argument type. Calling the function with an integer, a float, and a
string selects a different method in each case. The short syntax
f(x) = expression defines a method in the same way as a full
function ... end block.
$ julia main.jl integer: 10 float: 3.5 string: hi
More Specific Methods Win
Several methods may match a call. Julia chooses the most specific one — roughly, the method whose argument types are closest to the actual types.
f(x::Number) = "Number" f(x::Integer) = "Integer" f(x::Int) = "Int" println(f(3.0)) println(f(big(3))) println(f(3))
A Float64 is a Number, so only the first method
matches. A BigInt is an Integer but not an
Int, so the second method is used. An Int matches all
three methods, and the most specific one wins.
$ julia main.jl Number Integer Int
Dispatch on Multiple Arguments
Every argument participates in method selection. This is what "multiple" dispatch means.
combine(x::Int, y::Int) = "int-int" combine(x::Int, y::Float64) = "int-float" combine(x::Float64, y::Int) = "float-int" println(combine(1, 2)) println(combine(1, 2.0)) println(combine(1.0, 2))
The three methods have identical names but different pairs of argument types.
Julia examines both positions, so combine(1, 2.0) and
combine(1.0, 2) reach different methods even though both calls pass
one integer and one float.
$ julia main.jl int-int int-float float-int
A Catch-All Fallback
A method with no type restrictions acts as a fallback for values that no specialized method handles.
label(x) = "generic: $(typeof(x))"
label(x::Integer) = "integer: $x"
label(x::AbstractString) = "text: $(uppercase(x))"
label(x::Bool) = "boolean: $x"
for v in (7, 3.5, "hello", true, [1, 2])
println(label(v))
end
The unrestricted method handles the float and the array. The remaining values
use specialized methods. Note that Bool is a subtype of
Integer in Julia, so both methods match true; the
Bool method is more specific and is selected.
$ julia main.jl
integer: 7
generic: Float64
text: HELLO
boolean: true
generic: Vector{Int64}
Dispatching on Abstract Types
Methods can be written for abstract types, so one implementation serves a whole family of concrete types.
abstract type Shape end struct Circle <: Shape end struct Square <: Shape end area(::Circle) = 3.14 area(::Square) = 4.0 println(area(Circle())) println(area(Square()))
The anonymous argument syntax ::Circle means "an argument of type
Circle whose value we do not use". Each shape has its own method.
If we later wrote area(s::Shape), it would serve as a default for
any new subtype of Shape that does not define a more specific
method.
$ julia main.jl 3.14 4.0
Ambiguity
Sometimes neither of two matching methods is more specific. Julia detects this
at call time and raises a MethodError instead of guessing.
f(x::Number, y::Float64) = "first"
f(x::Float64, y::Number) = "second"
println(f(1, 2.0))
println(f(1.0, 2))
try
f(1.0, 2.0)
catch e
println(typeof(e))
end
For f(1, 2.0) only the first method matches, and for
f(1.0, 2) only the second one does. But f(1.0, 2.0)
matches both, and each is more specific in one position: neither is better
overall. The call is ambiguous, so Julia refuses to choose. Defining a
method for (Float64, Float64) would resolve it.
$ julia main.jl first second MethodError
Inspecting Methods
The methods function lists all definitions of a generic function,
ordered from most to least specific.
area(x::Number) = 0
area(r::Float64) = 1
for m in methods(area)
println(m.sig)
end
Each element yielded by methods is a Method object. Its
sig field is the method signature: a tuple whose first element is
the function type and whose remaining elements are the argument types. Here
the Float64 method appears before the broader Number
method. We print only the signature so the output does not depend on the file's
location on disk; printing a Method directly also shows its source file and
line number.
$ julia main.jl
Tuple{typeof(area), Float64}
Tuple{typeof(area), Number}
which and @which
To ask which method a particular call would use, use which or the
@which macro.
area(x::Number) = 0 area(r::Float64) = 1 using InteractiveUtils println(which(area, (Float64,)).sig) println(which(area, (Int,)).sig) m = @which area(2.0) println(m.sig)
which takes the function and a tuple of argument types and returns
the matching Method. @which is a convenient macro that takes an
ordinary call expression, area(2.0), and reports the same method;
assigned to a variable it returns it without printing. Both are provided by the
InteractiveUtils standard library, which must be imported in a
script. Used interactively, they display the source file and line of the
chosen method.
$ julia main.jl
Tuple{typeof(area), Float64}
Tuple{typeof(area), Number}
Tuple{typeof(area), Float64}
A Practical Example
Multiple dispatch lets one function name serve several natural definitions.
distance(a, b) = error("no distance defined")
distance(x::Int, y::Int) = abs(x - y)
distance(x::Tuple, y::Tuple) = sqrt(sum((x .- y).^2))
println(distance(3, 8))
println(distance((0, 0), (3, 4)))
The fallback method reports an error for unsupported pairs. Integers use
absolute difference; tuples use the Euclidean distance, with the broadcasting
operators .- and .^2 applied elementwise. Adding a new
kind of value means adding a new method, without changing the existing ones.
$ julia main.jl 5 5.0
Best Practices for Multiple Dispatch
A few habits make dispatch-based code easier to reason about.
- Keep methods of one function consistent: they should return compatible results and share one meaning.
- Dispatch on abstract types when possible: a method for
AbstractVectorcovers many concrete array types. - Provide a clear fallback: an unrestricted method can give a
helpful error instead of a generic
MethodError. - Resolve ambiguity explicitly: add a method for the overlapping concrete types rather than relying on method order.
Source
Julia Functions and Methods documentation
In this article we have seen Julia's multiple dispatch in action: methods
selected from all argument types, specificity, ambiguity, and introspection
with methods, which, and @which. Function
basics are covered in Julia functions; the type
hierarchy that dispatch relies on is explained in Julia abstract types, and the data types used
in signatures in Julia data types. Custom types
to dispatch on are defined in Julia structs.
Author
List all Julia tutorials.