# How to dispatch on an iterable (e.g. accept both tuple or array)

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 15, 2020, 10:27am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629 "2020-04-15T10:27:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [April 15, 2020, 10:27am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629/1 "2020-04-15T10:27:04Z")

</div>

Is it possible to dispatch on if a method argument is iterable? I’d like to dispatch a method based on if an argument is scalar input or some kind of iterable. My main target for the iterable method is to accept either an `Array` or a `Tuple`. Is that possible? Or do I need to use traits for this?

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [April 15, 2020, 11:59am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629/2 "2020-04-15T11:59:52Z")

</div>

Looks like some discussion on this already occurred:

[https://github.com/JuliaLang/julia/issues/34535](https://github.com/JuliaLang/julia/issues/34535)

[https://github.com/JuliaLang/julia/issues/23429](https://github.com/JuliaLang/julia/issues/23429)

And SimpleTraits has an `IsIterator` trait [https://github.com/mauro3/SimpleTraits.jl](https://github.com/mauro3/SimpleTraits.jl)

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [April 15, 2020, 12:09pm UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629/3 "2020-04-15T12:09:56Z")

</div>

The SimpleTraits.jl solution fails because numbers are iterable:

```julia
using SimpleTraits
using SimpleTraits.BaseTraits

@traitfn f(x::T) where {T; IsIterator{T}} = "iterable"
@traitfn f(x::T) where {T; !IsIterator{T}} = "not iterable"

```

I get

```julia
julia> f((1,2))
"iterable"

julia> f([1,2])
"iterable"

julia> struct A
       end

julia> f(A)
"not iterable"

```

which looks great, but then

```julia
julia> f(1.0)
"iterable"

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 15, 2020, 1:05pm UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629/4 "2020-04-15T13:05:32Z")

</div>

I don’t think this is feasible. In a lot of code `foo(itr::Any)` is the “`Any`” method, so iteration is the default and then `<: Number` is used for other things.

Not ideal, I guess. But you have to hope package authors appropriately sub-typed the types they made.

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [April 15, 2020, 9:38pm UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-iterable-e-g-accept-both-tuple-or-array/37629/5 "2020-04-15T21:38:22Z")

</div>

It turns out that SimpleTraits.jl can in fact solve this. See the reply to my issue: [https://github.com/mauro3/SimpleTraits.jl/issues/66#issuecomment-614289415](https://github.com/mauro3/SimpleTraits.jl/issues/66#issuecomment-614289415)
