From 9393f4adacbd7ec2d720a51befa6cd44b46f65fd Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 22 Feb 2017 13:34:02 -0800 Subject: [PATCH] Add IsIterable trait (#25) Add IsIterable trait --- src/base-traits.jl | 8 +++++++- test/base-traits.jl | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/base-traits.jl b/src/base-traits.jl index eb1ac3c..28f90ed 100644 --- a/src/base-traits.jl +++ b/src/base-traits.jl @@ -4,7 +4,7 @@ using SimpleTraits using Compat export IsLeafType, IsBits, IsImmutable, IsContiguous, IsIndexLinear, - IsAnything, IsNothing, IsCallable + IsAnything, IsNothing, IsCallable, IsIterable "Trait which contains all types" @traitdef IsAnything{X} @@ -57,4 +57,10 @@ end Base.@deprecate_binding IsFastLinearIndex IsIndexLinear +"Trait of all iterable types" +@traitdef IsIterable{X} +@generated function SimpleTraits.trait{X}(::Type{IsIterable{X}}) + method_exists(start, Tuple{X}) ? :(IsIterable{X}) : :(Not{IsIterable{X}}) +end + end # module diff --git a/test/base-traits.jl b/test/base-traits.jl index 3c66a4a..1eae587 100644 --- a/test/base-traits.jl +++ b/test/base-traits.jl @@ -30,3 +30,8 @@ if VERSION < v"0.5.0-dev" # this give deprecation warning in Julia 0.5 @test istrait(IsCallable{Base.AddFun}) end + +@test istrait(IsIterable{Array}) +@test !istrait(IsIterable{Cmd}) +@test istrait(IsIterable{Base.UnitRange{Int}}) +@test istrait(IsIterable{Base.UnitRange})