Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsIterable trait #25

Merged
merged 5 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/base-traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ using Compat
export IsLeafType, IsBits, IsImmutable, IsContiguous, IsIndexLinear,
IsAnything, IsNothing, IsCallable

@static if VERSION >= v"0.6.0-dev"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked that this does not work in 0.5?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I didn't, but it seems to work on 0.5 as well!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: My comment above about the new type system was related to a "complete" solution to this type of interface specification, as it is done in Traits.jl.

export IsIterable
end

"Trait which contains all types"
@traitdef IsAnything{X}
@traitimpl IsAnything{X} <- (x->true)(X)
Expand Down Expand Up @@ -57,4 +61,11 @@ end

Base.@deprecate_binding IsFastLinearIndex IsIndexLinear

@static if VERSION >= v"0.6.0-dev"
@traitdef IsIterable{X}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a tiny doc string.

@generated function SimpleTraits.trait{X}(::Type{IsIterable{X}})
method_exists(start, Tuple{X}) ? :(IsIterable{X}) : :(Not{IsIterable{X}})
end
end

end # module
5 changes: 5 additions & 0 deletions test/base-traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

if VERSION >= v"0.6.0-dev"
@test istrait(IsIterable{Array})
@test !istrait(IsIterable{Cmd})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add those two tests for good measure:

julia> method_exists(start, Tuple{Base.UnitRange{Int}})
true

julia> method_exists(start, Tuple{Base.UnitRange})
true

(I remember that parameterized methods sometimes tripped up method_exists. So in case there is a regression.)

end