Skip to content

Commit

Permalink
Fixed issue with dispatch_cache and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 committed Feb 19, 2018
1 parent 2ced60c commit 77d2eac
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/SimpleTraits.jl
Original file line number Diff line number Diff line change
@@ -187,7 +187,7 @@ end
dispatch_cache = Dict() # to ensure that the trait-dispatch function is defined only once per pair
let
global traitfn
function traitfn(tfn)
function traitfn(tfn, cur_module)
# Need
# f{X,Y}(x::X,Y::Y) = f(trait(Tr1{X,Y}), x, y)
# f(::False, x, y)= ...
@@ -300,7 +300,7 @@ let
end
# Create the trait dispatch function
ex = fn
key = (@__MODULE__, fname, typs0, strip_kw(args0), trait0_opposite)
key = (cur_module, fname, typs0, strip_kw(args0), trait0_opposite)
if !(key keys(dispatch_cache)) # define trait dispatch function
if !haskwargs
ex = quote
@@ -354,7 +354,11 @@ Defines a function dispatching on a trait. Examples:
Note that the second example is just syntax sugar for `@traitfn f{X,Y; Not{Tr1{X,Y}}}(x::X,y::Y) = ...`.
"""
macro traitfn(tfn)
esc(traitfn(tfn))
@static if isdefined(Base, Symbol("@__MODULE__"))
esc(traitfn(tfn, __module__))
else
esc(traitfn(tfn, current_module()))
end
end

######
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -245,6 +245,17 @@ isarrow(X) = eltype(X)<:Integer ? true : false
@test !istrait(TrArrow2{Vector{Int}})
@test istrait(TrArrow2{Vector{Float64}})

####
# issue with key in dispatch_cache
@traitfn f_dc(::::Tr1) = 1
module Mod
using SimpleTraits
using Base.Test
@traitdef Tr1{X}
@traitimpl Tr1{Integer}
@traitfn f_dc(::::Tr1) = 2
@test f_dc(1) == 2
end

######
# Other tests

0 comments on commit 77d2eac

Please # to comment.