Skip to content

Commit

Permalink
Fix IndexStyle and abstract depwarns on 0.6 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy authored and mauro3 committed Feb 20, 2017
1 parent 81a5d17 commit 21a075b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 0.5
MacroTools 0.3.2
Compat 0.19
9 changes: 7 additions & 2 deletions src/SimpleTraits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ __precompile__()

module SimpleTraits
using MacroTools
using Compat
const curmod = module_name(current_module())

# This is basically just adding a few convenience functions & macros
Expand All @@ -14,6 +15,8 @@ export Trait, istrait, @traitdef, @traitimpl, @traitfn, Not
## @doc """
## `abstract Trait{SUPER}`

@compat abstract type Trait end #{SUPER}

"""
All Traits are subtypes of abstract type Trait.
Expand All @@ -27,13 +30,15 @@ where X and Y are the types involved in the trait.
(SUPER is not used here but in Traits.jl, thus retained for possible
future compatibility.)
"""
abstract Trait #{SUPER}
Trait

@compat abstract type Not{T<:Trait} <: Trait end

"""
The set of all types not belonging to a trait is encoded by wrapping
it with Not{}, e.g. Not{Tr1{X,Y}}
"""
abstract Not{T<:Trait} <: Trait
Not

# Helper to strip an even number of Not{}s off: Not{Not{T}}->T
stripNot{T<:Trait}(::Type{T}) = T
Expand Down
19 changes: 12 additions & 7 deletions src/base-traits.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module BaseTraits
using SimpleTraits

export IsLeafType, IsBits, IsImmutable, IsContiguous, IsFastLinearIndex,
using Compat

export IsLeafType, IsBits, IsImmutable, IsContiguous, IsIndexLinear,
IsAnything, IsNothing, IsCallable

"Trait which contains all types"
@traitdef IsAnything{X}
@traitimpl IsAnything{X} <- (x->true)(X)

"Trait which contains no types"
typealias IsNothing{X} Not{IsAnything{X}}
@compat const IsNothing{X} = Not{IsAnything{X}}


"Trait of all isbits-types"
Expand All @@ -33,17 +35,17 @@ typealias IsNothing{X} Not{IsAnything{X}}
@traitimpl IsContiguous{X} <- Base.iscontiguous(X)

"Array indexing trait."
@traitdef IsFastLinearIndex{X} # https://github.com/JuliaLang/julia/pull/8432
function islinearfast(X)
if Base.linearindexing(X)==Base.LinearFast()
@traitdef IsIndexLinear{X} # https://github.com/JuliaLang/julia/pull/8432
function isindexlinear(X)
if IndexStyle(X)==IndexLinear()
return true
elseif Base.linearindexing(X)==Base.LinearSlow()
elseif IndexStyle(X)==IndexCartesian()
return false
else
error("Not recognized")
end
end
@traitimpl IsFastLinearIndex{X} <- islinearfast(X)
@traitimpl IsIndexLinear{X} <- isindexlinear(X)

# TODO
## @traitdef IsArray{X} # use for any array like type in the sense of container
Expand All @@ -52,4 +54,7 @@ end
## @traitdef IsMartix{X} # use for any LinearOperator
## # types<:AbstractArray are automatically part


Base.@deprecate_binding IsFastLinearIndex IsIndexLinear

end # module
4 changes: 2 additions & 2 deletions test/base-traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ c = view(a, 1:2:5)
@test istrait(IsContiguous{typeof(b)})
@test !istrait(IsContiguous{typeof(c)})

@test istrait(IsFastLinearIndex{Vector})
@test !istrait(IsFastLinearIndex{AbstractArray})
@test istrait(IsIndexLinear{Vector})
@test !istrait(IsIndexLinear{AbstractArray})

if VERSION < v"0.5.0-dev"
# this give deprecation warning in Julia 0.5
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SimpleTraits
using Compat
using Base.Test

const trait = SimpleTraits.trait
Expand Down Expand Up @@ -43,7 +44,7 @@ const trait = SimpleTraits.trait
@test trait(Tr2{Int, Float32})==Not{Tr2{Int, Float32}}

# issue 9
abstract A9
@compat abstract type A9 end
type B9<:A9 end
type C9<:A9 end
@traitdef Tr9{X}
Expand Down

0 comments on commit 21a075b

Please # to comment.