Skip to content

Commit

Permalink
Fixed bug with methods like f(::X) = ...
Browse files Browse the repository at this point in the history
Also reverted the helpdb.jl file
  • Loading branch information
mauro3 committed Sep 21, 2015
1 parent 816bb29 commit f674018
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
11 changes: 11 additions & 0 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3957,6 +3957,17 @@ Multiply elements of an array over the given dimensions.
"""
prod(A, dims)

doc"""
Base.linearindexing(A)
`linearindexing` defines how an AbstractArray most efficiently accesses its elements. If `Base.linearindexing(A)` returns `Base.LinearFast()`, this means that linear indexing with only one index is an efficient operation. If it instead returns `Base.LinearSlow()` (by default), this means that the array intrinsically accesses its elements with indices specified for every dimension. Since converting a linear index to multiple indexing subscripts is typically very expensive, this provides a traits-based mechanism to enable efficient generic code for all array types.
An abstract array subtype `MyArray` that wishes to opt into fast linear indexing behaviors should define `linearindexing` in the type-domain:
Base.linearindexing{T<:MyArray}(::Type{T}) = Base.LinearFast()
"""
Base.linearindexing

doc"""
isqrt(n)
Expand Down
4 changes: 4 additions & 0 deletions base/traits-bootstrap-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ a[1] = 5
@traitfn ggt{X; Tr1{X}}(::Type{X}, y) = (X,y)
@assert_ ggt(Array, 5)==(Array, 5)

# traitfn with ::X
@traitfn gg27{X; Tr1{X}}(::X) = X
@assert_ gg27(a)==Array{Int,1}

println("Traits tests done")
14 changes: 7 additions & 7 deletions base/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ macro traitfn(tfn)
fhead = tfn.args[1]
fbody = tfn.args[2]
fname = fhead.args[1].args[1]
args = [fhead.args[i] for i=2:arraylen(fhead.args)]
args = insertdummy([fhead.args[i] for i=2:arraylen(fhead.args)])
typs = [fhead.args[1].args[i] for i=3:arraylen(fhead.args[1].args)]
trait = fhead.args[1].args[2].args[1]
if isnegated(trait)
Expand All @@ -210,7 +210,7 @@ macro traitfn(tfn)
tmp1 = :($tmp1())
addtoargs!(tmp1, args)
tmp2 = :($fname($curmod.trait($trait)))
addtoargs!(tmp2, stripType(striparg(args)))
addtoargs!(tmp2, striparg(args))
fwrap = :($tmp1 = ($curmod.@_inline_meta(); $tmp2))

# The function containing the logic
Expand Down Expand Up @@ -248,14 +248,14 @@ end
isnegated(t::Expr) = t.head==:call

# [:(x::X)] -> [:x]
striparg{T}(args::Array{T,1}) = [striparg(args[i]) for i=1:arraylen(args)]
striparg{T}(args::Array{T,1}) = Any[striparg(args[i]) for i=1:arraylen(args)]
striparg(a::Symbol) = a
striparg(a::Expr) = a.args[1]

# :(Type{X}) -> X, X->X
stripType{T}(args::Array{T,1}) = [stripType(args[i]) for i=1:arraylen(args)]
stripType(a::Symbol) = a
stripType(a::Expr) = (a.head==:curly && a.args[1]==:Type) ? a.args[2] : a
# insert dummy: ::X -> gensym()::X
insertdummy{T}(args::Array{T,1}) = Any[insertdummy(args[i]) for i=1:arraylen(args)]
insertdummy(a::Symbol) = a
insertdummy(a::Expr) = (a.head==:(::) && arraylen(a.args)==1) ? Expr(:(::), gensym(), a.args[1]) : a

####
# Some trait definitions
Expand Down
4 changes: 4 additions & 0 deletions test/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ a[1] = 5
@traitfn ggt{X; Tr1{X}}(::Type{X}, y) = (X,y)
@test ggt(Array, 5)==(Array, 5)

# traitfn with ::X
@traitfn gg27{X; Tr1{X}}(::X) = X
@test gg27([1])==Array{Int,1}

######
# Other tests
#####
Expand Down

0 comments on commit f674018

Please sign in to comment.