liveBook · Manning

Method

As explained in Julia as a Second Language

A method is a function implementation associated with a particular argument-type signature and selected when a call matches that signature.

A method is an implementation of a function for a particular argument-type signature. In Julia, methods belong to functions rather than to types: one function can have several methods, each handling different arguments. A function without methods cannot perform an operation. section 7.3.3 section 7.4

When a function is called, Julia uses the types of all its arguments to select a matching method at runtime. This is multiple dispatch. It contrasts with single dispatch, where the implementation is selected using one argument’s type and may need additional tests for the others. section 7.4 section 7.4.1

Listing 7.8 Two methods for simulating battle between archers and knights
function attack!(a::Archer, b::Archer)          #1
   if a.arrows > 0                              #2
        shoot!(a)
        damage = 6 + rand(1:6)                  #3
        b.health = max(b.health - damage, 0)    #4
    end
    a.health, b.health
end
 
function attack!(a::Archer, b::Knight)          #5
    if a.arrows > 0                             #2
        shoot!(a)
        damage = rand(1:6)                      #6
        if b.mounted
            damage += 3
        end
        b.health = max(b.health - damage, 0)    #4
    end
    a.health, b.health
end

These two attack! methods share a function name but implement different rules for an archer attacking an archer or a knight. section 7.3.2

A signature can also use an abstract type. For example, battle!(a::Warrior, b::Warrior) accepts an Archer and a Knight because both are subtypes of Warrior. Its call to attack! can then select a method using the arguments’ concrete types. section 7.4.2

Listing 7.14 Writing out the outcome of a battle between two warriors
function battle!(a::Warrior, b::Warrior)      #1
   attack!(a, b)
    if a.health == 0 && b.health == 0
        println(a.name, " and ", b.name, " destroyed each other")
    elseif a.health == 0
        println(b.name, " defeated ", a.name)
    elseif b.health == 0
        println(a.name, " defeated ", b.name)
    else
        println(b.name, " survived attack from ", a.name)
    end
end

The battle! method uses a general Warrior signature while delegating the attack to another function. section 7.4.2

Use methods(attack!) at the REPL to inspect the signatures attached to a function. A MethodError means a function exists but has no matching method for the call; an UndefVarError can indicate that the function name itself is not defined. Add a suitable method to handle a missing argument combination. section 7.3.3

Methods can modify mutable objects: shoot!(archer::Archer) reduces the archer’s arrows. The exclamation mark is a naming convention for mutation. Returning the modified object can make REPL testing easier. section 7.3.2

How Julia as a Second Language covers it

How deep it goes
Explains method selection step by step and briefly describes JIT compilation and caching, while deferring compiler theory.
Examples
Demonstrates archer and knight attacks, a general warrior battle method, and REPL inspection of method signatures.
What you can do after
Apply methods in Julia to define type-specific behavior, inspect signatures, and address missing-method errors.
How it connects
Connects methods to multiple dispatch, type hierarchies, mutation, single dispatch, and function overloading.

Also explained in

Compare how 22 books explain method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.