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 the code attached to a function for a particular argument-type signature. In Julia, a function can have several methods, each handling a different combination of arguments. The methods belong to the function, not to the types of the objects passed to it. section 7.3.3

One attack, different opponents

Suppose an archer attacks another archer or a knight. Define both behaviors under the name attack!: `attack!(a::Archer, b::Archer)` and `attack!(a::Archer, b::Knight)`. Each definition supplies its own code. The first can calculate damage to an archer; the second can account for whether the knight is mounted. Calling `attack!(a, b)` runs the method matching the types of both arguments. section 7.3.2 section 7.4

Try `methods(attack!)` in the REPL to see its available signatures. A function defined as `function attack! end` exists but has no behavior yet: calling it raises a `MethodError`. An undefined name instead raises an `UndefVarError`. If no attack method matches two knights, add `attack!(a::Knight, b::Knight)`; the same function then handles that combination too. section 7.3.3

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

Compare the two `attack!` definitions: their shared name and distinct argument signatures select different battle rules. section 7.3.2

This selection is multiple dispatch: the types of all arguments matter. It differs from the illustrated object-oriented single-dispatch pattern, which chooses behavior from one object's type and may need conditionals for other argument types. It also differs from compile-time function overloading: a call inside `battle!(a::Warrior, b::Warrior)` can select an `attack!` method at runtime using the concrete types supplied. section 7.4.1 section 7.4.2

A method can accept a shared abstract type: `battle!(a::Warrior, b::Warrior)` matches an archer and a knight because both are subtypes of `Warrior`. For methods that change an object's fields, the `!` suffix signals mutation by convention; the relevant composite type must be mutable. Returning the changed object makes the result easy to inspect in the REPL. section 7.4.2 section 7.3.2

How Julia as a Second Language covers it

How deep it goes
Works through definitions, inspection, missing-method errors, and runtime selection; sketches compilation while deferring compiler theory.
Examples
Shows typed archer-and-knight attacks, REPL method listings and errors, and a battle function accepting abstract warrior arguments.
What it assumes
Introduces function-versus-method terminology through typed composite objects and REPL calls; defers detailed compiler theory.
What you can do after
Build it from scratch by adding typed methods to a Julia function and checking which argument combinations it supports.
How it connects
Connects methods to functions, composite types, multiple dispatch, single dispatch, abstract types, and function overloading.

Also explained in

Compare how 20 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.