RGB has a length but no iterate?

I’m defining a handy function for me to flatten data recursively into matrices and tensors. One way to do it is to descend into the list-of-lists until we see something with length(x)==1, and then we consider that to be the element type of our tensor.

There’s a problem with this method, though. If my type is RGB{Float64}, this isn’t actually considered an “atomic” element, because length(RGB(rand(3)...))==3. On the other hand, there is no iterate method for RGB. So the approach fails because I’ve found something that has a length but cannot be iterated.

What is a better way to detect if a julia type is iterable or not? In principle, I would actually be able to choose between considering tuples as iterables or elementes. I don’t think there’s a isiterable method in Julia. And in fact, I’m not even sure why there’s a length(Float64), I can imagine there are types out there that have to length method, and I would like to support those as well.

So questions are:

  1. Is RGB inconsistent by offering a length but no iterate? And
  2. What are the best ways to decide if a type is iterable or not?

Here’s another type that I would like to understand better. Tuple has iterate and length, but no ndims. Int32 and Range has everything defined.

Just throwing out there GitHub - rafaqz/Flatten.jl: Flatten nested Julia objects to tuples, and reconstruct them later in case you didn’t know about.