Super type for all things iterable?


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



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

ERROR: MethodError: no method matching iterate(::MyType)


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.


function f(itr::TypeForThingsIterable)
    #perform some operations



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


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


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