mauro3 / Traits.jl

Exploration of traits in Julia
Other
39 stars 6 forks source link

`@traitdef in one module, `@traitimpl in another module #31

Open bjmtrk opened 8 years ago

bjmtrk commented 8 years ago

Hi,

i was trying to do this, but obviously i get the following issue:

julia> module X
        using Traits
        export TR
        @traitdef TR{T} begin
            fun(T) -> Number
        end
       end
  This warning is ok:
WARNING: Method definition func_for_method(Method, Any, Any) in module Inference at inference.jl:610 overwritten in module Traits at /local/rra/HEJU/julia_pkg/Traits.jl/src/base_fixes.jl:8.
WARNING: Method definition eltype(Type{Base.Associative{#K<:Any, #V<:Any}}) in module Base at dict.jl:240 overwritten in module Traits at /local/rra/HEJU/julia_pkg/Traits.jl/src/base_fixes.jl:20.
  endof ok-warnings.
X

julia> module Y
           using Traits
           using X
           @traitimpl TR{Int64} begin
               fun(x::Int64) = 3x
           end
       end
**** Checking X.TR{T}: Could not instantiate instance for type encoding the trait X.TR{Int64}.
This usually indicates that something is amiss with the @traitdef
or that one of the generic functions is not defined.
The error was: UndefVarError(:fun)
ERROR: AssertionError: istrait(TR{Int64},verbose=true)

How can i do this? Thanks

(Edited by @mauro3: added tripple back-ticks. Also note that with quoting macro I meant enclosing them with back-ticks and not just a single back-tick in front of them.)

mauro3 commented 8 years ago

It should work, but doesn't... This workaround works:

julia> module X
       using Traits
       export TR
       fun(x)= 1
       @traitdef TR{T} begin
           fun(T) -> Number
       end
       end
X

julia> module Y
       using Traits
       using X
       @traitimpl TR{Int64} begin
           fun(x::Int64) = 3x
       end
       end

Maybe an explicit function def should be used? Like so:

julia> module X
       using Traits
       export TR
       function fun end # not sure whether an explicit function-def 
       @traitdef TR{T} begin
           fun(T) -> Number
       end
       end

But this does not work either. However, in light of https://github.com/JuliaLang/julia/issues/14636, I'm not sure how to implement this.