-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
bug in dispatch #18985
Comments
That's because f{T<:Union{Float32,Float64}}(x::T, y, z...) = (println(length(z)+1); f(y, z...)) |
That said, a redefinition warning should probably be printed in that case. |
isn't the vararg signature less specialized than the other one, so that f(x) should always be preferred to f(x, y...)? Also, the fact that T<:Number works and T<:Union is clearly an incoherent behaviour (bug?) |
I'm curious: what's the subtype of FYI, using julia> typealias F32F64 Union{Float32, Float64}
Union{Float32,Float64}
julia> f{T<:F32F64}(x::T) = "hi"
f (generic function with 1 method)
julia> f{T<:F32F64}(x::T, y...) = (println(length(y)); f(y[1], y[2:end]...))
f (generic function with 2 methods)
julia> f(1.,rand(10)...)
10
9
8
7
6
5
4
3
2
1
"hi" |
I think this is a dispatch error. This is different from the default argument case and there are only two methods and the second one should always be more specific than the first one. Of course, defining it the way @nalimilan mentioned above is still preferred since it's easier to handle for the compiler and is less likely to cause ambiguity when there's other methods. |
just to point to a real case scenario, I encountered this issue here JuliaCollections/Iterators.jl#85 while trying to define |
I believe this is fixed by #18457 - @CarloLucibello can you confirm? |
It's working now, nice! @JeffBezanson julia> f{T<:Union{Float32,Float64}}(x::T, y...) = (println(length(y)); f(y[1], y[2:end]...))
f (generic function with 1 method)
julia> f{T<:Union{Float32,Float64}}(x::T) = "hi"
f (generic function with 2 methods)
julia> f(1.,rand(10)...)
10
9
8
7
6
5
4
3
2
1
"hi" |
The following code works correctly
But it fails when I change to <:Union the parametrization
At the last step we have a wrong dispatch to the first method instead of the second
Bye,
Carlo
The text was updated successfully, but these errors were encountered: