# Super type for all things iterable?

**URL:** https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765
**Category:** General Usage
**Created:** [November 20, 2018, 12:26pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765 "2018-11-20T12:26:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [November 20, 2018, 12:26pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765/1 "2018-11-20T12:26:59Z")

</div>

Is there such a type? Or is there a way that I can check whether something is iterable?

Thanks

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [November 20, 2018, 12:37pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765/2 "2018-11-20T12:37:38Z")

</div>

No. You could check with `hasmethod` though. But most of the times it doesn’t matter. The question is: what do you want to do with the things not iterable? If there is no fallback strategy for things that are not iterable in your case, then it is often not harmful to just call `iterate` and let the function fail naturally with a method error if a non-iterable is passed as argument. This is how it is done in functions such as

```julia
mean(MyType())
ERROR: MethodError: no method matching iterate(::MyType)

```

---

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [November 20, 2018, 12:40pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765/3 "2018-11-20T12:40:48Z")

</div>

I want to collect all things iterable and convert them into an array to perform some operations, as for things not iterable, I simply want to ignore them.

Thanks

```nohighlight
function f(itr::TypeForThingsIterable)
    arr=collect(itr)
    #perform some operations
end

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [November 20, 2018, 1:05pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765/4 "2018-11-20T13:05:03Z")

</div>

Maybe:

```julia
julia> function f(itr::T) where T
       if hasmethod(iterate, Tuple{T})
         # do something
       else
         # do nothing
       end
       end

```

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [November 20, 2018, 3:14pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765/5 "2018-11-20T15:14:33Z")

</div>

`hasmethod` is not pure, so you get a runtime check of the method table. This probably wants to be a trait instead.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [November 20, 2018, 7:04pm UTC](https://discourse.julialang.org/t/super-type-for-all-things-iterable/17765/6 "2018-11-20T19:04:08Z")

</div>

But if you want to make it an automatic trait which does static dispatch, you’d have to use a generated function, which should not use `hasmethod` (although I’m guilty of doing this myself [https://github.com/mauro3/SimpleTraits.jl/blob/916feab13502003d25e9c04e352aaced4d2509cc/src/base-traits.jl#L69](https://github.com/mauro3/SimpleTraits.jl/blob/916feab13502003d25e9c04e352aaced4d2509cc/src/base-traits.jl#L69)).
