Method priority rules

Please explain methods priority or specificity in different cases. Where in Julia documentation is it explained? The following line and example in documentation are contradictory.

The order in which the methods are defined does not matter and neither is more specific than the other.

while here in Methods · The Julia Language

julia> same_type(x::T, y::T) where {T} = true
same_type (generic function with 1 method)

julia> same_type(x,y) = false
same_type (generic function with 2 methods)

the first method is specific or preferred. Why not following code gives false as output if no method is specific or has priority?

julia> same_type(1, 2)

I found this post but it explain different case. :backhand_index_pointing_down:

A method can be more specific than another, but when that’s not true, they risk a call throwing an ambiguity MethodError. You’re taking that quote out of a context where ambiguous methods were defined. I’ll save everyone else a click for an accurate quote:

Here the call g(2.0, 3.0) could be handled by either the g(::Float64, ::Any) or the g(::Any, ::Float64) method. The order in which the methods are defined does not matter and neither is more specific than the other. In such cases, Julia raises a MethodError rather than arbitrarily picking a method.

1 Like