Skip to content

IteratorSize incorrect for some subtypes of Generator #58109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nsajko opened this issue Apr 14, 2025 · 0 comments · May be fixed by #58110
Open

IteratorSize incorrect for some subtypes of Generator #58109

nsajko opened this issue Apr 14, 2025 · 0 comments · May be fixed by #58110
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior iteration Involves iteration or the iteration protocol

Comments

@nsajko
Copy link
Contributor

nsajko commented Apr 14, 2025

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.

@nsajko nsajko added bug Indicates an unexpected problem or unintended behavior iteration Involves iteration or the iteration protocol labels Apr 14, 2025
@nsajko nsajko self-assigned this Apr 14, 2025
nsajko added a commit to nsajko/julia that referenced this issue Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior iteration Involves iteration or the iteration protocol
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant