Skip to content

Commit afcec3b

Browse files
committed
Fixes #9
1 parent b8e3bca commit afcec3b

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/SimpleTraits.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ Example:
114114
macro traitimpl(tr)
115115
# makes
116116
# trait{X1<:Int,X2<:Float64}(::Type{Tr1{X1,X2}}) = Tr1{X1,X2}
117+
if tr.args[1]==:Not || isnegated(tr)
118+
tr = tr.args[2]
119+
negated = true
120+
else
121+
negated = false
122+
end
117123
typs = tr.args[2:end]
118124
trname = esc(tr.args[1])
119125
curly = Any[]
@@ -125,10 +131,18 @@ macro traitimpl(tr)
125131
arg = :(::Type{$trname{$(paras...)}})
126132
fnhead = :($curmod.trait{$(curly...)}($arg))
127133
isfnhead = :($curmod.istrait{$(curly...)}($arg))
128-
quote
129-
$fnhead = $trname{$(paras...)}
130-
$isfnhead = true # Add the istrait definition as otherwise
131-
# method-caching can be an issue.
134+
if !negated
135+
return quote
136+
$fnhead = $trname{$(paras...)}
137+
$isfnhead = true # Add the istrait definition as otherwise
138+
# method-caching can be an issue.
139+
end
140+
else
141+
return quote
142+
$fnhead = Not{$trname{$(paras...)}}
143+
$isfnhead = false# Add the istrait definition as otherwise
144+
# method-caching can be an issue.
145+
end
132146
end
133147
end
134148

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ trait = SimpleTraits.trait
4242
@test trait(Tr2{Int, Float64})==Tr2{Int, Float64}
4343
@test trait(Tr2{Int, Float32})==Not{Tr2{Int, Float32}}
4444

45+
# issue 9
46+
abstract A9
47+
type B9<:A9 end
48+
type C9<:A9 end
49+
@traitdef Tr9{X}
50+
@traitimpl Tr9{A9}
51+
@traitimpl Not{Tr9{B9}}
52+
@traitimpl !Tr9{C9}
53+
@test istrait(Tr9{A9})==true
54+
@test istrait(Tr9{B9})==false
55+
@test istrait(Tr9{C9})==false
56+
4557
#################
4658
# Trait functions
4759
#################

0 commit comments

Comments
 (0)