JuliaLang/julia

Methoderror for constructors should consider typevars

Open

#33.539 aperta il 12 ott 2019

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)Julia (5773 fork)batch import
error messageshelp wanted

Metriche repository

Star
 (48.709 star)
Metriche merge PR
 (Merge medio 20g 6h) (157 PR mergiate in 30 g)

Descrizione

Consider

julia> struct Foo{T,S}
           a::S
           b::T
       end

julia> Foo(1, 2)
Foo{Int64,Int64}(1, 2)

julia> Foo{Int64, Int64}(1, 2)
Foo{Int64,Int64}(1, 2)

julia> Foo{Int64}(1)
ERROR: MethodError: no method matching Foo{Int64,S} where S(::Int64)
Stacktrace:
 [1] top-level scope at REPL[4]:1

The MethodError here is missing closest candidates for the implicitly defined constructors. The reason for that is likely the following:

julia> methods(Foo{Int64})
# 0 methods for type constructor:

julia> methods(Foo)
# 1 method for type constructor:
[1] (::Type{Foo})(a::S, b::T) where {T, S} in Main at REPL[1]:2

julia> methods(Base.unwrap_unionall(Foo))
# 2 methods for type constructor:
[1] (::Type{Foo})(a::S, b::T) where {T, S} in Main at REPL[1]:2
[2] (::Type{Foo{T,S}})(a, b) where {T, S} in Main at REPL[1]:

so one approach to start here might be widening f in https://github.com/JuliaLang/julia/blob/master/base/errorshow.jl#L350 to Base.unwrap_unionall(Base.unwrap_unionall(Foo{Int64}).name.wrapper) or something similar.

Guida contributor