Skip to content
LumenaryAcademy
Foundations 60 min

Functions and multiple dispatch

Every way to write a function in Julia, and the idea at the centre of the language, choosing code by the types of all the arguments.

Code tested · 3AI-draftedAwaiting review by a computer scientist
MotivationH

Functions own nothing

In Python, Java or C#, behaviour usually lives inside a class. To ask a shape for its area you write shape.area(), and the code that runs is chosen by looking at one object, the one before the dot. That works well until an operation genuinely involves two or more objects equally. Which class should own the collision of an asteroid with a spaceship? Which number type should own the addition of an integer and a rational? Object-oriented languages answer with workarounds such as double dispatch, visitor patterns and long chains of isinstance checks.

Julia takes a different route. Functions do not belong to types. A function is just a name, like area or +, and it may have many methods, each written for a particular combination of argument types. When you call the function, Julia looks at the types of all the arguments and runs the most specific method that fits. This is multiple dispatch, and it is the single idea that most shapes how Julia code is written and why Julia packages combine so well. Before we get there, we need the everyday mechanics of functions.

Worked examplesH

Writing and calling functions

julia
# Ways to define and call functions

# Short form, for one-line definitions
bmi(mass_kg, height_m) = mass_kg / height_m^2

# Long form, with a default argument and keyword arguments after the semicolon
function describe(name, value; unit = "", digits = 1)
    text = "$name: $(round(value; digits))"
    return isempty(unit) ? text : "$text $unit"
end

# Several results come back as a tuple, which you can destructure
function min_max_mean(xs)
    return minimum(xs), maximum(xs), sum(xs) / length(xs)
end

# Varargs collect any number of arguments into a tuple
total_minutes(durations...) = sum(durations)

println(describe("BMI", bmi(68, 1.75)))
println(describe("Marathon", 42.195; unit = "km"))
println(describe("Everest", 8848.86; unit = "m", digits = 0))
lo, hi, avg = min_max_mean([12, 7, 30, 19])
println("lo = $lo, hi = $hi, mean = $avg")
println(total_minutes(45, 30, 20), " minutes")
times = [10, 15, 5]
println(total_minutes(times...), " minutes (splatted from a vector)")

# Anonymous functions and higher-order functions
println(map(x -> x^2, 1:5))
println(filter(iseven, 1:10))
println(reduce(+, 1:100))
println(sum(abs2, [3, 4]))                  # many functions take a function first

# Composition and piping
shout = uppercase ∘ strip                   # \circ then Tab
println(shout("   karibu   "))
println([4, 9, 16] |> x -> sqrt.(x) |> sum)

# do-block syntax passes a multi-line anonymous function as the first argument
grades = map([55, 72, 91, 38]) do score
    if score >= 90
        "A"
    elseif score >= 70
        "B"
    elseif score >= 50
        "C"
    else
        "retake"
    end
end
println(grades)

# A closure remembers the variables around it
function make_counter()
    count = 0
    return () -> (count += 1)
end
tick = make_counter()
tick(); tick()
println("ticked ", tick(), " times")

# The ! convention: sort returns a new vector, sort! changes its argument
v = [3, 1, 2]
w = sort(v)
println(v, " ", w)
sort!(v)
println(v)

It prints:

text
BMI: 22.2
Marathon: 42.2 km
Everest: 8849.0 m
lo = 7, hi = 30, mean = 17.0
95 minutes
30 minutes (splatted from a vector)
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
5050
25
KARIBU
9.0
["C", "B", "A", "retake"]
ticked 3 times
[3, 1, 2] [1, 2, 3]
[1, 2, 3]

Here is what each part shows.

  • Short form. bmi(m, h) = m / h^2 defines a function in one line. It is the same as the long function ... end form; Julia programmers use the short form freely for small functions.
  • Keyword arguments come after a semicolon in the definition and are passed by name: describe("Marathon", 42.195; unit = "km"). They are ideal for options with sensible defaults. round(value; digits) is shorthand for round(value; digits = digits). Note that round with digits keeps a floating-point result, which is why Everest shows as 8849.0.
  • Multiple return values are really one tuple, which you can destructure: lo, hi, avg = min_max_mean(...).
  • Varargs. durations... in a definition collects the arguments into a tuple; times... in a call splats a collection into separate arguments.
  • Anonymous functions such as x -> x^2 are values you can pass around. map, filter, reduce and many others take a function as their first argument, a convention throughout Julia, and sum(abs2, xs) applies abs2 to each element before summing.
  • Composition and pipes. f ∘ g is the function that applies g and then f; x |> f is f(x). Use them where they make a pipeline easier to read, not everywhere.
  • do-blocks. Because functions are passed first, Julia offers a special syntax: map(xs) do x ... end creates an anonymous function from the block and passes it as map's first argument. You will see it constantly with files, as in open("data.txt") do io ... end, where the file is closed for you when the block ends.
  • Closures capture the variables around them. make_counter returns a function that keeps its own private count.
  • The ! convention. A function whose name ends in ! modifies one or more of its arguments, usually the first. sort(v) returns a sorted copy; sort!(v) sorts v in place. The ! is only a naming convention, not enforced by the language, but the whole ecosystem follows it, and so should you.

Julia passes arguments by sharing: a function receives the same object the caller has, not a copy. Assigning a new value to an argument name inside a function does not affect the caller, but mutating the object (as sort! does) does. This is how Python and Java behave too.

DefinitionsH

Generic functions and methods

Definition— Generic function, method, multiple dispatch

A generic function is a named function such as area or +. A method is one definition of it for a particular signature: a number of arguments and, optionally, their types. Multiple dispatch is the rule that, at each call, Julia selects the method whose signature matches the types of all the arguments most specifically.

Every time you write a definition, you add a method to a generic function, creating the function if needed. The REPL tells you how many methods it now has:

text
julia> area(w, h) = w * h
area (generic function with 1 method)

julia> area(r) = π * r^2
area (generic function with 2 methods)

julia> area(3, 4)
12

julia> area(1.0)
3.141592653589793

julia> methods(area)
# 2 methods for generic function "area" from Main:
 [1] area(r)
     @ REPL:1
 [2] area(w, h)
     @ REPL:1

julia> combine(a::Int, b) = "first method"
combine (generic function with 1 method)

julia> combine(a, b::Int) = "second method"
combine (generic function with 2 methods)

julia> combine(1, 2)
ERROR: MethodError: combine(::Int64, ::Int64) is ambiguous.

Candidates:
  combine(a, b::Int64)
    @ Main REPL:1
  combine(a::Int64, b)
    @ Main REPL:1

Possible fix, define
  combine(::Int64, ::Int64)

"Most specific" has a precise meaning: method A is more specific than method B if A's signature is a subtype of B's. A method for (Int64, Int64) beats one for (Integer, Real), which beats one for (Any, Any). Arguments without annotations are of type Any, so they match everything and lose to anything more particular.

The last part of the session shows the one way dispatch can fail besides finding no method at all. combine(1, 2) matches both combine(a::Int, b) and combine(a, b::Int), and neither is more specific than the other: each is more specific in a different argument. Julia refuses to guess and reports an ambiguity, helpfully suggesting the fix: define a method for the overlap, combine(::Int64, ::Int64).

Dispatch on every argument

Rock, paper, scissors is a two-player game, so who wins depends on both hands at once, exactly the situation that is awkward in single-dispatch languages. Here each hand is a type. struct Rock end declares a new concrete type with no fields (lesson 5 covers structs properly), and abstract type Hand end gives the three a common supertype. An argument written as ::Rock, with no name, matches any value of type Rock without binding it to a variable.

julia
# Multiple dispatch: the method is chosen by the types of *all* arguments.
abstract type Hand end
struct Rock <: Hand end
struct Paper <: Hand end
struct Scissors <: Hand end

beats(::Rock, ::Scissors) = true
beats(::Scissors, ::Paper) = true
beats(::Paper, ::Rock) = true
beats(::Hand, ::Hand) = false          # every other pairing

function play(a::Hand, b::Hand)
    beats(a, b) && return "$(nameof(typeof(a))) wins"
    beats(b, a) && return "$(nameof(typeof(b))) wins"
    return "draw"
end

for (a, b) in [(Rock(), Scissors()), (Rock(), Paper()), (Paper(), Paper())]
    println(nameof(typeof(a)), " vs ", nameof(typeof(b)), ": ", play(a, b))
end

println("beats has ", length(methods(beats)), " methods")

# Later, perhaps in another package, someone adds a new hand. Nothing above changes.
struct Well <: Hand end                # rock and scissors fall into the well; paper covers it
beats(::Well, ::Rock) = true
beats(::Well, ::Scissors) = true
beats(::Paper, ::Well) = true
println("Well vs Rock: ", play(Well(), Rock()))
println("Paper vs Well: ", play(Paper(), Well()))

It prints:

text
Rock vs Scissors: Rock wins
Rock vs Paper: Paper wins
Paper vs Paper: draw
beats has 4 methods
Well vs Rock: Well wins
Paper vs Well: Paper wins

Look at the shape of this code. There is no if a isa Rock && b isa Scissors anywhere. Each rule of the game is one small method, and the fallback beats(::Hand, ::Hand) = false covers every pairing nobody mentioned, because it is the least specific. Then the program adds a fourth hand, Well, with three new methods, and play handles it correctly without being edited. In a real project those new methods could live in a different file or a different package, written by someone who never touched the original code. This is a small version of what Julia programmers mean when they say that multiple dispatch lets packages extend each other.

IntuitionH

Why generic code composes

Here is the other half of the story. Write a function without type annotations and it works for any argument that supports the operations it uses. Horner's rule evaluates a polynomial c1+c2x+c3x2+⋯c_1 + c_2x + c_3x^2 + \cdots by working from the highest coefficient down, using only multiplication and addition:

julia
# One generic function, many types: Horner's rule for a polynomial
# c[1] + c[2]x + c[3]x² + ... evaluated with only multiplications and additions.
function horner(coeffs, x)
    result = zero(x)
    for c in reverse(coeffs)
        result = result * x + c * one(x)     # one(x) is 1 for numbers, I for matrices
    end
    return result
end

p = [1, -3, 2]                         # 1 - 3x + 2x²
println(horner(p, 2))                  # Int64
println(horner(p, 0.5))                # Float64
println(horner(p, 1//3))               # exact Rational
println(horner(p, 1 + 1im))            # Complex
println(horner(p, big(10)^20))          # BigInt: no overflow
A = [1 1; 0 1]
println(horner(p, A))                  # works for a matrix too: I - 3A + 2A²

# A more specific method for one type wins over the generic one
horner(coeffs, x::Bool) = x ? sum(coeffs) : first(coeffs)
println(horner(p, true), " ", horner(p, false))
println(length(methods(horner)), " methods for horner")

It prints:

text
3
0.0
2//9
-2 + 1im
19999999999999999999700000000000000000001
[0 1; 0 0]
0 1
2 methods for horner

One definition handled integers, floats, exact rationals, complex numbers, arbitrary-precision integers (no overflow for x=1020x = 10^{20}) and even a matrix, where it computed I−3A+2A2I - 3A + 2A^2. The only concession to generality is zero(x) and one(x), which give the additive and multiplicative identities for whatever type x has: 0 and 1 for numbers, the zero and identity matrices for matrices. For each new type, Julia compiled a separate specialised version, so the Float64 call runs as fast as a hand-written Float64 loop.

Now combine the two halves. Generic code like horner works on any type with the right operations, and multiple dispatch lets anyone give a new type those operations. So when a package defines, say, a number type that carries measurement uncertainty, or a "dual number" that computes derivatives automatically, horner works on it immediately, along with thousands of other generic functions its author never heard of. Stefan Karpinski's JuliaCon 2019 talk The Unreasonable Effectiveness of Multiple Dispatch explores this at length, and it is why Julia packages for differential equations, automatic differentiation, units and GPUs can be combined in ways none of their authors planned.

The final lines of the program also show specificity at work: horner(coeffs, x::Bool) is more specific than horner(coeffs, x) for a Bool, so it wins for true and false and leaves every other type to the generic method.

ExplanationH

Dispatch as professionals use it

Extend Base functions for your types. The most common use of multiple dispatch in everyday code is not inventing new functions but adding methods to existing ones. To make your type print nicely, add a method to Base.show; to make it work with +, add a method to Base.:+; to make it iterable in a for loop, add methods to Base.iterate. You must either write the module name (Base.show(io::IO, x::MyType) = ...) or import Base: show first, so that you extend Julia's function rather than create an unrelated one with the same name. Lesson 5 does this for real.

Do not commit type piracy. Adding a method to a function you do not own, for types you do not own, is called type piracy, for example defining Base.:+(a::String, b::String) in your package. It changes the behaviour of everybody else's code that happens to load yours, and it is the cause of some of the most confusing bugs in the ecosystem. The rule: when you extend a function from elsewhere, at least one of the argument types should be yours.

Annotate for dispatch, not for speed. As the previous lesson explained, annotations do not make code faster. Use them where you need different behaviour for different types, and otherwise prefer generic code. A function that accepts x::Real instead of x::Float64 costs nothing and works for far more inputs.

Keyword arguments do not take part in dispatch. Methods are selected by positional arguments only. If two behaviours differ by an option, make it a keyword; if they differ by the kind of thing passed, make it a method.

Explore dispatch in the REPL. Because so much of Julia is methods of generic functions, the tools for inspecting them are worth learning early. methods(f) lists every method of f (in a plain Julia 1.13 session, + already has 155 of them, one for each combination of number, array, date and other types that Base knows how to add). @which 1 + 2.0 shows which method a particular call would use and where it is defined, and @edit 1 + 2.0 opens that source in your editor. Reading Base's own methods this way is one of the best ways to learn idiomatic Julia, because Base is written in Julia.

Order arguments the way the ecosystem does. Functions taking a function put it first (so that do-blocks work); functions that mutate put the mutated argument first, right after any function argument (push!(v, x), map!(f, dest, src)); and the things a function is "about" come before configuration, which usually becomes keywords. Following these conventions makes your functions feel like part of the language.

Names. Functions and variables are written in lowercase, with underscores only when a name would be hard to read without them (isempty, haskey, min_max_mean); types and modules use CamelCase. Predicates often start with is or has.

Common misconceptionsH

Misconceptions

Misconception

"Multiple dispatch is just function overloading." Overloading in C++ or Java picks a function at compile time using the declared types of the variables. Julia picks a method at run time using the actual types of the values (and, when it can prove the types in advance, the compiler resolves the choice early, so there is usually no run-time cost). The difference matters: in Julia, a value held in a variable of abstract type still dispatches on its concrete type.

Misconception

"The ! makes a function mutate its argument." The ! is part of the name and nothing more. It is a promise the author makes to readers, which is why breaking it is so confusing. Equally, a function without ! should not mutate its arguments.

Misconception

"To dispatch on two arguments I need a class that owns the operation." No class owns anything in Julia. beats(::Paper, ::Rock) is a method of beats, not of Paper or Rock, and you can add it from anywhere.

Exercises

Exercises

1. Functions and multiple dispatch

CoreComputational

Given these three methods:

g(x::Number) = "number"
g(x::Integer) = "integer"
g(x) = "anything"

what does println(g(2.0), " ", g(2), " ", g("2")) print?

2. Functions and multiple dispatch

Warm-upComputational

What does this print?

f(x; scale = 2) = x * scale
println(f(3; scale = 5), " ", f(3))

3. Functions and multiple dispatch

Warm-upComputational

What does println(map(x -> x^2, filter(isodd, 1:6))) print?

4. Functions and multiple dispatch

CoreComputational

foldl(op, xs) combines elements from the left: op(op(op(x1, x2), x3), x4). What does println(foldl(-, 1:4)) print?

5. Functions and multiple dispatch

CoreComputational

What does this print?

count_args(xs...) = length(xs)
println(count_args([1, 2, 3]...), " ", count_args([1, 2, 3]))

6. Functions and multiple dispatch

CoreComputational

What does this print?

function make_acc(start)
    total = start
    return x -> (total += x)
end
acc = make_acc(10)
acc(5)
println(acc(1))

7. Functions and multiple dispatch

StretchComputational

With only the methods h(a::Int, b) = 1 and h(a, b::Int) = 2 defined, what happens when you call h(1, 2)?

Choose one

8. Functions and multiple dispatch

CoreHuman

Your package defines a type Vec2. Which of these method definitions in your package is type piracy?

Choose one

Further reading and resources