# RGB has a length but no iterate?

**URL:** <https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446>\
**Category:** General Usage\
**Tags:** type, collection, iterators\
**Created:** [July 19, 2022, 8:31am UTC](https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446 "2022-07-19T08:31:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [July 19, 2022, 8:31am UTC](https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446/1 "2022-07-19T08:31:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [July 19, 2022, 8:46am UTC](https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446/2 "2022-07-19T08:46:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 19, 2022, 9:34am UTC](https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446/3 "2022-07-19T09:34:36Z")

</div>

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