Skip to content
This repository was archived by the owner on Feb 7, 2019. It is now read-only.

Commit a23dc3c

Browse files
committed
added workaround example for issue #2
1 parent 92b0b37 commit a23dc3c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

examples/ex_issue2.jl

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
######
2+
# workaround for issue #2
3+
##########
4+
using Traits
5+
6+
@traitdef Foo_NotWorking{X} begin
7+
fnxx{Y<:Integer}(X, Vector{Y}) -> Integer
8+
end
9+
10+
# This errors because of https://github.com/JuliaLang/julia/issues/9043
11+
#
12+
# @traitimpl Foo_NotWorking{Float64} begin
13+
# fnxx(x::Float64, y::Array{Uint8,1}) = y[iround(x)]
14+
# end
15+
16+
@traitdef Foo{X} begin
17+
Y = getFooY(X)
18+
getFooY(Type{X}) -> DataType
19+
fnx(X, Y) -> Integer
20+
@constraints begin
21+
eltype(Y)<:Integer
22+
end
23+
end
24+
25+
@traitimpl Foo{Float64} begin
26+
getFooY(::Type{Float64}) = Array{Uint8,1}
27+
fnx(x::Float64, y::Array{Uint8,1}) = y[iround(x)]
28+
end
29+
30+
@traitimpl Foo{Int} begin
31+
getFooY(::Type{Int}) = Array{Integer,1}
32+
fnx(x::Int, y::Array{Integer,1}) = y[iround(x)]
33+
end
34+
35+
@traitfn tf89{X, Y<:Integer; Foo{X}}(x::X, a::Vector{Y}) = fnx(x, a) + 5
36+
37+
tf89(2., Uint8[1,2])
38+
tf89(2, Integer[1,2])
39+
40+
# tf89(2., Int[1,2]) # errors
41+
42+
## or this also works:
43+
44+
@traitdef Bar{X} begin
45+
Y = getBarY(X)
46+
getBarY(Type{X}) -> DataType
47+
gnx(X, Vector{Y}) -> Integer
48+
@constraints begin
49+
eltype(Y)<:Integer
50+
end
51+
end
52+
53+
@traitimpl Bar{Float64} begin
54+
getBarY(::Type{Float64}) = Uint8
55+
gnx(x::Float64, y::Array{Uint8,1}) = y[iround(x)]
56+
end
57+
58+
@traitimpl Bar{Int} begin
59+
getBarY(::Type{Int}) = Integer
60+
gnx(x::Int, y::Array{Integer,1}) = y[iround(x)]
61+
end
62+
63+
@traitfn tf90{X, Y<:Integer; Bar{X}}(x::X, a::Vector{Y}) = gnx(x, a) + 5
64+
65+
tf90(2., Uint8[1,2])
66+
tf90(2, Integer[1,2])
67+
68+
# tf90(2., Int[1,2]) # errors

0 commit comments

Comments
 (0)