mauro3 / SimpleTraits.jl

Simple Traits for Julia
MIT License
159 stars 14 forks source link

Error in dispatch? #38

Closed sbromberger closed 7 years ago

sbromberger commented 7 years ago
julia> using SimpleTraits

julia> abstract type AbFoo end

julia> struct Foo <: AbFoo
    x::Int
end

julia> @traitdef IsDirected{G<:AbFoo}

julia> @traitimpl IsDirected{G} <- is_directed(G)

julia> is_directed(x...) = error("nope")
is_directed (generic function with 1 method)

julia> is_directed(::Foo) = true
is_directed (generic function with 2 methods)

julia> is_directed(::Type{Foo}) = true
is_directed (generic function with 3 methods)

julia> is_directed(Foo)
true

julia> is_directed(AbFoo)
ERROR: nope
Stacktrace:
 [1] is_directed(::Type{T} where T) at ./REPL[6]:1

julia> @traitfn f(g::::IsDirected) = g.x
f (generic function with 2 methods)

julia> f(Foo(10))
ERROR: MethodError: no method matching is_directed(::Type{Foo})
The applicable method may be too new: running in world age 21602, while current world is 21607.
Closest candidates are:
  is_directed(::Type{Foo}) at REPL[8]:1 (method too new to be called from this world context.)
  is_directed(::Any...) at REPL[6]:1 (method too new to be called from this world context.)
  is_directed(::Foo) at REPL[7]:1 (method too new to be called from this world context.)
Stacktrace:
 [1] trait(...) at /Users/seth/.julia/v0.6/SimpleTraits/src/SimpleTraits.jl:186
 [2] f(::Foo) at /Users/seth/.julia/v0.6/SimpleTraits/src/SimpleTraits.jl:319

julia> is_directed(Foo)
true

julia> f(Foo(10))
ERROR: MethodError: no method matching is_directed(::Type{Foo})
The applicable method may be too new: running in world age 21602, while current world is 21608.
Closest candidates are:
  is_directed(::Type{Foo}) at REPL[15]:1 (method too new to be called from this world context.)
  is_directed(::Any...) at REPL[6]:1 (method too new to be called from this world context.)
  is_directed(::Foo) at REPL[7]:1 (method too new to be called from this world context.)
Stacktrace:
 [1] trait(...) at /Users/seth/.julia/v0.6/SimpleTraits/src/SimpleTraits.jl:186
 [2] f(::Foo) at /Users/seth/.julia/v0.6/SimpleTraits/src/SimpleTraits.jl:319

julia> is_directed(Foo)
true
sbromberger commented 7 years ago

ref #39

mauro3 commented 7 years ago

Yes, this should be fixed with #39.

mauro3 commented 7 years ago

Fix. Ref https://github.com/mauro3/SimpleTraits.jl/pull/39#issuecomment-296496286