Skip to content

Error getting length of Iterator.flatten with tuples and arrays of different lengths #59261

@zot

Description

@zot

It works for tuples of the same length:

julia> length(Iterators.flatten(((1,2),(3,4))))
4

It fails for arrays of the same or different lengths, tuples of different lengths, and generators of the same length. Here are some errors:

julia> length(Iterators.flatten(([1,2],[3,4])))
ERROR: ArgumentError: Iterates of the argument to Flatten are not known to have constant length
Stacktrace:
 [1] flatten_length(f::Base.Iterators.Flatten{Tuple{Vector{Int64}, Vector{Int64}}}, T::Type)
   @ Base.Iterators ./iterators.jl:1241
 [2] length(f::Base.Iterators.Flatten{Tuple{Vector{Int64}, Vector{Int64}}})
   @ Base.Iterators ./iterators.jl:1243
 [3] top-level scope
   @ REPL[26]:1

julia> length(Iterators.flatten(([1,2],[3])))
ERROR: ArgumentError: Iterates of the argument to Flatten are not known to have constant length
Stacktrace:
 [1] flatten_length(f::Base.Iterators.Flatten{Tuple{Vector{Int64}, Vector{Int64}}}, T::Type)
   @ Base.Iterators ./iterators.jl:1241
 [2] length(f::Base.Iterators.Flatten{Tuple{Vector{Int64}, Vector{Int64}}})
   @ Base.Iterators ./iterators.jl:1243
 [3] top-level scope
   @ REPL[22]:1

julia> length(Iterators.flatten(((1,),(2,3))))
ERROR: ArgumentError: Iterates of the argument to Flatten are not known to have constant length
Stacktrace:
 [1] flatten_length(f::Base.Iterators.Flatten{Tuple{Tuple{Int64}, Tuple{Int64, Int64}}}, T::Type)
   @ Base.Iterators ./iterators.jl:1241
 [2] length(f::Base.Iterators.Flatten{Tuple{Tuple{Int64}, Tuple{Int64, Int64}}})
   @ Base.Iterators ./iterators.jl:1243
 [3] top-level scope
   @ REPL[33]:1

julia> length(Iterators.flatten(((i for i in (1,2)), (i for i in (3,4)))))
ERROR: ArgumentError: Iterates of the argument to Flatten are not known to have constant length
Stacktrace:
 [1] flatten_length(f::Base.Iterators.Flatten{Tuple{Base.Generator{…}, Base.Generator{…}}}, T::Type)
   @ Base.Iterators ./iterators.jl:1241
 [2] length(f::Base.Iterators.Flatten{Tuple{Base.Generator{…}, Base.Generator{…}}})
   @ Base.Iterators ./iterators.jl:1243
 [3] top-level scope
   @ REPL[25]:1
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    iterationInvolves iteration or the iteration protocol

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions