Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wishlist: document method specificity rules #23740

Open
timholy opened this issue Sep 17, 2017 · 2 comments
Open

Wishlist: document method specificity rules #23740

timholy opened this issue Sep 17, 2017 · 2 comments
Assignees
Labels
docs This change adds or pertains to documentation needs docs Documentation for this change is required types and dispatch Types, subtyping and method dispatch

Comments

@timholy
Copy link
Sponsor Member

timholy commented Sep 17, 2017

There are a number of situations in which Julia could choose to throw an ambiguity error, but instead establishes rules for prioritizing some types over others. In early "experimental" days the only real source for this information was the C code, but now that we're stabilizing it seems appropriate to document those rules. (Perhaps that documentation exists, but with a few-minute search I was unable to find it.)

Motivated by the question at https://discourse.julialang.org/t/method-dispatch-for-tuples/5925.

@timholy timholy added the needs docs Documentation for this change is required label Sep 17, 2017
@JeffBezanson JeffBezanson added docs This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch labels Sep 19, 2017
@llbit
Copy link

llbit commented Jan 16, 2024

Here is a case of method specificity that I find puzzling and which does not seem to be explained in the current documentation:

foo(::Type{T}, x::T) where T = 1
foo(::Type{T}, x::Number) where {T<:Number} = 2

A call to foo(Int, 1) returns 2. My mental model of parameterized methods was that they were instantiated before dispatch/specificity but this seems to not be the case. I have not looked at the implementation.

@vchuravy
Copy link
Member

Specificity comes before instantiation

julia> Tuple{Type{T}, T} where {T<:Number} <: Tuple{Type{T}, T} where {T}
true

julia> Tuple{Type{T}, T} where {T} <: Tuple{Type{T}, T} where {T<:Number}
false

e.g. foo(::Type{T}, x::T) where T = 1 is foo(::Type{T}, x::T) where {T<:Any} = 1 thus T<:Number is a more specific version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation needs docs Documentation for this change is required types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

4 participants