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

incorrect method sorting with union in typevar bound #8915

Closed
srp opened this issue Nov 6, 2014 · 3 comments
Closed

incorrect method sorting with union in typevar bound #8915

srp opened this issue Nov 6, 2014 · 3 comments
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@srp
Copy link
Contributor

srp commented Nov 6, 2014

The documentation states that methods are selected based off which is the most specific. While not explicitly stated, this generally also seems to be true of constructors. However on 0.4, the following seems to be functioning off the order the constructors are declared in, rather then which is most specific:

type A{O <: Union(Float64,String)}
    A(a) = println("A(a)")
    A(a::Int) = println("A(a::Int)")
end

type B{O <: Union(Float64,String)}
    B(a::Int) = println("B(a::Int)")
    B(a) = println("B(a)")
end

A{Float64}(1)
B{Float64}(1)

Which produces the following output:

A(a)
B(a::Int)

I can't reproduce this on 0.3, only 0.4. I've also only been able to reproduce this when parameterizing the type with a Union.

@simonster
Copy link
Member

This seems like a general method sorting bug not restricted to constructors:

julia> type A{T} end

julia> f{T<:Union(Float64,Int)}(::Type{A{T}}, a) = false;

julia> f{T<:Union(Float64,Int)}(::Type{A{T}}, a::Int) = true;

julia> f(A{Float64}, 1)
false

julia> methods(f)
# 2 methods for generic function "f":
f{T<:Union(Float64,Int64)}(::Type{A{T<:Union(Float64,Int64)}},a) at none:1
f{T<:Union(Float64,Int64)}(::Type{A{T<:Union(Float64,Int64)}},a::Int64) at none:1

In this form it's reproducible on 0.3 as well.

@srp srp changed the title Constructor selection based off method order if type parameterized off Union Method/constructor selection isn't working right Nov 7, 2014
@JeffBezanson JeffBezanson changed the title Method/constructor selection isn't working right incorrect method sorting with union in typevar bound Nov 7, 2014
@JeffBezanson JeffBezanson added the types and dispatch Types, subtyping and method dispatch label Jan 28, 2016
@timholy
Copy link
Member

timholy commented Sep 7, 2016

Another likely instance of this can be seen in ColorTypes, depending on whether we precompile or not:

tim@diva:~/.julia/v0.5/ColorTypes/test$ julia -q
julia> using ColorTypes

julia> Gray(0.8)
Gray{Float64}(0.8)

julia> 
tim@diva:~/.julia/v0.5/ColorTypes/test$ julia -q
julia> include("../src/ColorTypes.jl")
ColorTypes

julia> using ColorTypes

julia> Gray(0.8)
------------------------------------------------------------------------------------------
StackOverflowError                                      Stacktrace (most recent call last)
[#1] — ColorTypes.Gray{T<:Union{AbstractFloat,Bool,FixedPointNumbers.FixedPoint}}(::Float64)
       ⌙ at types.jl:369 (repeats 80000 times)

StackOverflowError:

@JeffBezanson
Copy link
Member

closed by #18457

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

4 participants