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

Commit

Permalink
looked into a few issues, in particular #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 committed Apr 9, 2015
1 parent 53b3a4b commit d0321b2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Feature updates for Traits.jl
=============================

30 March 2015
-------------

- Allowing symbols and isbits as associated types.

14 Nov 2014
-----------

Expand Down
16 changes: 13 additions & 3 deletions src/traitdef.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ function parsefnstypes!(outfns, ln)
# Parse to get function signature.
# parses f(X,Y), f{X <:T}(X,Y) and X+Y
tvars = Any[]
if isa(def.args[1], Symbol)
if isa(def.args[1], Symbol) # f(X,Y)
fn = def.args[1]
elseif def.args[1].head==:curly
elseif def.args[1].head==:curly # f{X}(X,Y)
fn = def.args[1].args[1]
# get
tvars = def.args[1].args[2:end]
Expand Down Expand Up @@ -195,7 +195,17 @@ function parsefnstypes!(outfns, ln)
"Something went wrong parsing the trait definition body with line:\n$ln"))
end
# replace types with constraints by TypeVars
trans = Dict(zip([t.args[1] for t in tvars], tvars)) # this will error if there is a type-var without constraints!
tmp = Any[]
for t in tvars
if isa(t,Symbol)
#error("Having a ")
push!(tmp,t)
else
push!(tmp,t.args[1])
end
end
# trans = Dict(zip([t.args[1] for t in tvars], tvars)) # this will error if there is a type-var without constraints!
trans = Dict(zip(tmp,tvars))
translate!(argtype.args, trans)
tvar2tvar!(argtype.args)
subt2tvar!(rettype.args)
Expand Down
2 changes: 1 addition & 1 deletion test/perf/perf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ end
include("traits_vs_ducks.jl")

# load all examples from
dispatch_bug1 = true # needs to be synchronized with ../runtests.jl:dispatch_bug1
include("../traitdispatch.jl")


println("""\n\nAny warnings following this line can indicate code-generation problems:""")
# the bounds used below are from my Linux i7, Julia commit 01a3216*,
# Traits commit 6d9cc07e9
Expand Down
9 changes: 6 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ type A2 end

@test !istraittype(A1)

# BUG flags: set to false once fixed to activate tests
method_exists_bug = true # see https://github.com/JuliaLang/julia/issues/8959
method_exists_bug2 = true # see https://github.com/JuliaLang/julia/issues/9043
## BUG flags: set to false once fixed to activate tests
# Julia issues:
method_exists_bug1 = true # see https://github.com/JuliaLang/julia/issues/8959
method_exists_bug2 = true # see https://github.com/JuliaLang/julia/issues/9043 and https://github.com/mauro3/Traits.jl/issues/2
# Traits.jl issues:
dispatch_bug1 = true # in traitdispatch.jl

# manual implementations
include("manual-traitdef.jl")
Expand Down
18 changes: 15 additions & 3 deletions test/traitdef.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ end

coll = [Vector{Int}, Dict{Int,Int}, Set{Int}]
iter = [Traits.GenerateTypeVars{:upcase}, Int] #todo: add String,
if method_exists_bug
if method_exists_bug1
assoc = [] #todo add again: Dict{Int,Int}] # , ObjectIdDict]
else
index = [Array{Int,2}, Dict{Int,Int}, StepRange{Int,Int}]
assoc = [Array{Int,2}, Dict{Int,Int}, StepRange{Int,Int}]
end
index = [Array{Int,2}, StepRange{Int,Int}]

Expand Down Expand Up @@ -166,7 +166,9 @@ end

#--> need to be able to do this in terms of type variables.

# test functions parameterized on non-trait parameters
# test functions parameterized on non-trait parameters. This isn't currently working:
# https://github.com/mauro3/Traits.jl/issues/2
# https://github.com/JuliaLang/julia/issues/9043

@traitdef Pr0{X} begin
fn75{Y <: Integer}(X, Y) -> Y
Expand All @@ -193,6 +195,16 @@ else
end
@test !istrait(Pr1{UInt8})

@traitdef Pr2{X} begin
fn77{Y<:Number}(X,Y,Y) -> Y
# fn77{Y}(X)
end
fn77(a::Array,b::Int, c::Float64) = a[1]
if method_exists_bug2
@test !istrait(Pr2{Array})
else
@test istrait(Pr2{Array})
end
# test constraints

@traitdef Cr20{X} begin
Expand Down
8 changes: 6 additions & 2 deletions test/traitdispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ end
@traitfn tttf238{X; UU1{X}}(x::X) = "this should loose"
@traitfn tttf238{X; UU2{X}}(x::X) = "this should win"

println("This test in traitdispatch.jl should probably pass, fix dispatch and change here.")
@test_throws Traits.TraitException tttf238(5)=="this should win"
# This test in traitdispatch.jl should probably pass:
if dispatch_bug1
@test_throws Traits.TraitException tttf238(5)=="this should win"
else
@test tttf238(5)=="this should win"
end

# however if U2 were a subtrait of something else but U1 then dispatch should be ambiguous:
@traitdef V1{X} begin
Expand Down
2 changes: 1 addition & 1 deletion test/traitfns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ a = yt1(5,6)
@traitfn xt1{X<:Int,Y<:FloatingPoint; Arith{X,Y}}(x::X,y::Y) = x-y
@test_throws MethodError xt1(5,6)

if !(method_exists_bug)
if !(method_exists_bug1)
od = ObjectIdDict(); od[5]=8
@traitfn yt1{X,Y; Assoc{X}}(x::X,y::Y) = x==y
@test yt1(Dict(5=>7), Dict(5=>8))==false
Expand Down

0 comments on commit d0321b2

Please sign in to comment.