I have a recursive function f(x, data) which can return anywhere between 1 and length(x) elements. All I want to be able to do is iterate over the return value of f so the result doesn’t need to be instantiated into a Tuple, for example, but I can’t figure out how to return an iterator over the result, or whether I even need to worry about it.
My reason for asking is that I could simply have the returns be wrapped in Tuple, but the length of a Tuple is part of its type, and so the function would not be stable. I’m looking to not allocate a vector, and not have type instability. How can I do this?
The general allocation-free way to do this is to define your own type struct FooIterator with an iterate method. For example, see the combinations function in Combinatorics.jl. This may require a bit of rethinking of how your function works, since it needs to be rearranged to compute return values one at a time as iterate is called.
Otherwise you should just return an array if you want a dynamically sized container, not a tuple. (No need to optimize allocations unless they are performance-critical, and type instability is generally worse than array allocations.)