Skip to content

Commit 9393f4a

Browse files
davidanthoffmauro3
authored andcommitted
Add IsIterable trait (#25)
Add IsIterable trait
1 parent 21a075b commit 9393f4a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/base-traits.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using SimpleTraits
44
using Compat
55

66
export IsLeafType, IsBits, IsImmutable, IsContiguous, IsIndexLinear,
7-
IsAnything, IsNothing, IsCallable
7+
IsAnything, IsNothing, IsCallable, IsIterable
88

99
"Trait which contains all types"
1010
@traitdef IsAnything{X}
@@ -57,4 +57,10 @@ end
5757

5858
Base.@deprecate_binding IsFastLinearIndex IsIndexLinear
5959

60+
"Trait of all iterable types"
61+
@traitdef IsIterable{X}
62+
@generated function SimpleTraits.trait{X}(::Type{IsIterable{X}})
63+
method_exists(start, Tuple{X}) ? :(IsIterable{X}) : :(Not{IsIterable{X}})
64+
end
65+
6066
end # module

test/base-traits.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ if VERSION < v"0.5.0-dev"
3030
# this give deprecation warning in Julia 0.5
3131
@test istrait(IsCallable{Base.AddFun})
3232
end
33+
34+
@test istrait(IsIterable{Array})
35+
@test !istrait(IsIterable{Cmd})
36+
@test istrait(IsIterable{Base.UnitRange{Int}})
37+
@test istrait(IsIterable{Base.UnitRange})

0 commit comments

Comments
 (0)