This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
traitfns.jl
246 lines (206 loc) · 6.79 KB
/
traitfns.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# tests parsing in src/traitfns.jl
import Base: ==
f1e = :(f1{X<:Int,TT; D1{X}, D2{X,TT}}(x::X,y::TT))
f1e_b= :(f1{X<:Int,TT; D1{X}, D2{X,TT}}(x::X,y::TT) = ())
f1e_function = :(function f1{X<:Int,TT; D1{X}, D2{X,TT}}(x::X,y::TT)
()
end)
f1e_p = Traits.ParsedFn(
:f1,
:(f1{X<:Int,TT}),
Any[Expr(:<:, :X, :Int),:TT],
Any[:(x::X), :(y::TT)],
Any[:(D1{X}), :(D2{X,TT})],
:(()))
f1e_pt = Traits.ParsedFn(
:f1,
:(f1{X1<:Int,X2}),
Any[Expr(:<:, :X1, :Int),:X2],
Any[:(x1::X1), :(x2::X2)],
Any[:(D1{X1}), :(D2{X1,X2})],
:(()))
function ==(p::Traits.ParsedFn, q::Traits.ParsedFn)
out = true
for n in fieldnames(p)
if n==:body # tricky to compare...
continue
end
out = out && getfield(p,n)==getfield(q,n)
if !out
@show n, getfield(p,n), getfield(q,n)
end
end
out
end
@test Traits.parsetraitfn_head(f1e)==f1e_p
@test Traits.translate_head(Traits.parsetraitfn_head(f1e))==f1e_pt
@test Traits.parsetraitfn(f1e_b)==(f1e_p, f1e_pt)
(a,b) = Traits.parsetraitfn(f1e_function)
a.body =:()
b.body =:()
@test a==f1e_p
@test b==f1e_pt
@test Traits.makefnhead(f1e_p.name, f1e_p.typs, f1e_p.sig).args[1]==f1e_p.fun
@test Traits.makefnhead(f1e_p.name, f1e_p.typs, f1e_p.sig).args[2:end]==f1e_p.sig
@test Traits.makefncall(f1e_p.name, f1e_p.sig)==:(f1(x,y))
@test Traits.get_concrete_type_symb(f1e_p.typs)==Any[:Int, :Any]
@test Traits.make_Type_sig([s.args[2] for s in f1e_p.sig])==Any[:(::Type{X}), :(::Type{TT})]
# next case
f1e2= :(f1{X<:Int; D1{X}, D2{X,X}}(x::X,y::X) = ())
f1e2_pt = Traits.ParsedFn(
:f1,
:(f1{X1<:Int}),
Any[Expr(:<:, :X1, :Int)],
Any[:(x1::X1), :(x2::X1)],
Any[:(D1{X1}), :(D2{X1,X1})],
:(()))
@test Traits.parsetraitfn(f1e2)[2]==f1e2_pt
# next case
f1ee = :(tfd{K, V; D2{K,V}}(x::Vector{K}, y::V) = ())
f1ee_p = Traits.ParsedFn(
:tfd,
:(tfd{K,V}),
Any[:K,:V],
Any[:(x::Vector{K}), :(y::V)],
Any[:(D2{K,V})],
:(()))
f1ee_pt = Traits.ParsedFn(
:tfd,
:(tfd{X1,X2}),
Any[:X1,:X2],
Any[:(x1::Vector{X1}), :(x2::X2)],
Any[:(D2{X1,X2})],
:(()))
@test Traits.parsetraitfn(f1ee)==(f1ee_p, f1ee_pt)
# next case
f1ee2 = :(tfd{K, V; D2{K,V}}(x::Dict{K,Int}, y::V) = ())
f1ee2_p = Traits.ParsedFn(
:tfd,
:(tfd{K,V}),
Any[:K,:V],
Any[:(x::Dict{K,Int}), :(y::V)],
Any[:(D2{K,V})],
:(()))
f1ee2_pt = Traits.ParsedFn(
:tfd,
:(tfd{X1,X2}),
Any[:X1,:X2],
Any[:(x1::Dict{X1,Int}), :(x2::X2)],
Any[:(D2{X1,X2})],
:(()))
@test Traits.parsetraitfn(f1ee2)==(f1ee2_p, f1ee2_pt)
# next case
f1d2 = :(tfd{K, V; D2{K,V}}(x::Dict{K,Int}, y::V, z::Int) = ())
f1d2_p = Traits.ParsedFn(
:tfd,
:(tfd{K,V}),
Any[:K,:V],
Any[:(x::Dict{K,Int}), :(y::V), :(z::Int)],
Any[:(D2{K,V})],
:(()))
f1d2_pt = Traits.ParsedFn(
:tfd,
:(tfd{X1,X2}),
Any[:X1,:X2],
Any[:(x1::Dict{X1,Int}), :(x2::X2), :(x3::Int)],
Any[:(D2{X1,X2})],
:(()))
@test Traits.parsetraitfn(f1d2)==(f1d2_p, f1d2_pt)
# trait function creation
eval(:(@traitfn $f1e_b))
eval(:(@traitfn $f1e_function))
eval(:(@traitfn $f1e2))
# these give warnings for now
eval(:(@traitfn $f1ee))
eval(:(@traitfn $f1ee2))
eval(:(@traitfn $f1d2))
@traitfn yt1{X,Y; Arith{X,Y}}(x::X,y::Y) = x+y
a = yt1(5,6)
@test yt1(5,6)==5+6
@traitfn xt1{X<:Int,Y<:AbstractFloat; Arith{X,Y}}(x::X,y::Y) = x-y
@test_throws MethodError xt1(5,6)
od = ObjectIdDict(); od[5]=8
@traitfn yt1{X,Y; IsAssociative{X}}(x::X,y::Y) = x==y
@test yt1(Dict(5=>7), Dict(5=>8))==false
@traitfn xt1{X<:Dict,Y<:ObjectIdDict; IsAssociative{X}}(x::X,y::Y) = x==y
@test xt1(Dict(5=>7), od)==false
@test_throws MethodError xt1(od, od)
# mixing with normal methods:
xt1(x::Int, y::Int) = 77
@test xt1(5,6)==77
# scope of helper methods when defined in different modules
####
## case 1
module Mod1
using Traits
# to check what happens with exports, etc
export MTyp1, M1Tr1, tf1, barbar
type MTyp1
mt::Int
end
@traitdef M1Tr1{X} begin
barbar(X, Int) -> AbstractString
end
@traitimpl M1Tr1{MTyp1} begin
barbar(a::MTyp1, b::Int) = "MTyp1, barbar"^b
end
@traitfn tf1{X, Y<:Int; M1Tr1{X}}(a::X, b::Y) = barbar(a, b)
# a normal generic function to be used later
f1(x::Int) = 5x
end # module Tst
using Mod1
@test tf1(MTyp1(7), 5)=="MTyp1, barbar"^5
# now try to extend tf1
# 1) add a new type to M1Tr1
type MTyp99
uu::Int
end
@traitimpl M1Tr1{MTyp99} begin
barbar(a::MTyp99, b::Int) = "MTyp99, barbar"^b
end
@test tf1(MTyp99(9), 5)=="MTyp99, barbar"^5
@test tf1(Main.MTyp99(9), 5)=="MTyp99, barbar"^5
# 2) add a new method to tf1
@traitdef M1Tr100{X} begin
foofoo(X) -> AbstractString
end
type MTyp00
gg::Int
end
@traitimpl M1Tr100{MTyp00} begin
foofoo(a::MTyp00) = "MTyp00 foofoo"
end
import Mod1.tf1
# @show tf1(Traits._TraitStorage, MTyp1, Int)
# @show tf1(Traits._TraitStorage,Any,Int)
# typs = Any[:(Traits._TraitStorage),:X, Expr(:<:, :X1, :Int)]
# methods(eval(:tf1), Traits. get_concrete_type_Typetuple(typs))
println(" These warnings are ok:") # Well, I'm not sure whether they are ok. But at least normal...
@traitfn tf1{X, Y<:Int; M1Tr100{X}}(a::X, b::Y) = foofoo(a)^b
println(" endof ok-warnings.")
#methods(eval(:tf1), Traits. get_concrete_type_Typetuple(typs))
#@show tf1(Traits._TraitStorage, Any, Int)
@test tf1(MTyp1(7), 5)=="MTyp1, barbar"^5
@test tf1(MTyp00(9), 5)== "MTyp00 foofoo"^5
# @show tf1(Traits._TraitStorage, MTyp1, Int)
# @show tf1(Traits._TraitStorage, Any, Int)
# # error:
@test tf1(MTyp99(9), 5)=="MTyp99, barbar"^5
# case 2: adding a @traitfn to a generic function defined in another module
@traitfn tf1_2{X; M1Tr100{X}}(a::X) = foofoo(a)^99
############
# error
@traitfn ffgg{T; Eq{T,T}}(x::T,y::T) = 2x==y
@test ffgg(5,6)==false
@test ffgg(6,12)==true
######
# traitmethods in modules
@traitfn ff879{T; Eq{T,T}}(x::T,y::T) = 2x==y
@test length(traitmethods(ff879))==1
module A9374
using Traits
using Base.Test
@traitfn ff879{T; Eq{T,T}}(x::T,y::T) = 2x==y
@test length(traitmethods(ff879))==1
end
@test length(traitmethods(ff879))==1