Is this a legal use of Zip?

I would like to create a collection of iterators. Before I start iterating I would like to access individual iterators. Doing that requires accessing the field of Zip. Does this look okay?

N = 3
it1 = fill(1, N)
it2 = fill(2, N)
z =  zip(it1, it2)
@show z.is[1]

AFAIK, fields are usually considered an implementation detail and are not part of the API. So your code currently works and will likely continue to work for a long time, but technically it is possible that some future version of Julia changes the internals of zip and your code breaks.

I would recommend that instead of passing a zip() object, you pass a tuple of iterators and then you only zip() them once you start iterating.

N = 3
it1 = fill(1, N)
it2 = fill(2, N)

iters = (it1,it2)
# Do what you need to do with iters[i], then iterate using
for i in zip(iters...)
    # Work on i
end
1 Like

That sounds like good advice.