Purposes of explicit type annotations

Sure, but that’s not what’s being discussed in that section. Including a few more lines of context:

Thus, one can write many useful Julia functions without ever explicitly using types. When additional expressiveness is needed, however, it is easy to gradually introduce explicit type annotations into previously “untyped” code. Adding annotations serves three primary purposes: to take advantage of Julia’s powerful multiple-dispatch mechanism, to improve human readability, and to catch programmer errors.

The point being made here is that the functions f(x::Vector{Int}) and g(x::Vector{<:Integer}) and h(x::AbstractVector) and k(x) will all give the same performance (for the same input). The fact that a function has a type annotation on its input is useful as a way to control multiple dispatch or signal to a user what kind of data they should provide, but it’s not necessary for performance in Julia.

Another way of putting this is that the performance of a function does depend on the values you provide to it (as in the Int vs UInt8 example you mention), but it generally does not depend on whether or not you provided a type annotation to its arguments when you defined that function.

8 Likes