Skip to content

IteratorSize incorrect for some subtypes of Generator #58109

Closed
@nsajko

Description

@nsajko
julia> collection_of_iterators = [Iterators.map(identity, 3), Iterators.map(sqrt, 3)];

julia> Base.IteratorSize.(collection_of_iterators)
2-element Vector{Base.HasShape{0}}:
 Base.HasShape{0}()
 Base.HasShape{0}()

julia> Base.IteratorSize(eltype(collection_of_iterators))  # incorrect, hits the fallback method
Base.HasLength()

There are two correct options for the return type here:

  • HasShape{0}: precise and accurate
  • SizeUnknown: imprecise, but still accurate

The fix should be simple, this is the relevant method, just need to relax the dispatch:

IteratorSize(::Type{Generator{I,F}}) where {I,F} = IteratorSize(I)

NB: This is perhaps part of a wider issue of HasLength not being a correct fallback, but that's a separate topic.

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behavioriterationInvolves iteration or the iteration protocol

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions