Annotating Types: Best practice for beginners

  • Annotate types inside of type definitions for performance.
  • Annotate types inside of function definitions to exploit multiple dispatch or to prevent duck typing.

Basically, this is good for performance:

struct Foo
    x::Int64
    y::Float64
end

And this is bad:

struct Foo
    x
    y
end

In contrast, performance is the same for either of these when called on z of type Foo:

function bar(z)
    z.x + z.y
end
function bar(z::Foo)
    z.x + z.y
end
23 Likes