Skip to content
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

Introduce iterator trait: IsStateful #43388

Open
jakobnissen opened this issue Dec 10, 2021 · 0 comments
Open

Introduce iterator trait: IsStateful #43388

jakobnissen opened this issue Dec 10, 2021 · 0 comments
Labels
design Design of APIs or of the language itself feature Indicates new feature / enhancement requests iteration Involves iteration or the iteration protocol

Comments

@jakobnissen
Copy link
Contributor

jakobnissen commented Dec 10, 2021

Currently, Julia generally assumes iterators are stateless. This is clearly wrong, and so leads to issues:

I have run into some of these issues myself, and they cause bugs that are quite annoying to track down.
Also, a few days ago, @kescobo reported this:

julia> lines = (i for i in eachline(IOBuffer("a\nb")));

julia> iterate(lines)
("a", nothing)

julia> isempty(lines)
false

julia> iterate(lines)[1]
ERROR: MethodError: no method matching getindex(::Nothing, ::Int64)

Previously, these issues has been resolved by the band-aid approach: Wait until the erroneous assumption that iterators are stateless causes problems, then special-case those problems.
I think @kescobo 's issue is a good example why this is not viable: The number of possible stateful iterators are infinite. We need a more principled approach.

I propose we make a trait that tells if an iterator has state or not. It defaults to IsStateless. We implement IsStateful for a few basic stateful iterators, and then iterators built on top of other stateful iterators are also stateful.
We can then make functions like isempty error for stateful iterators, unless isdone has been defined.

@oxinabox proposed something similar in #27412.

@nsajko nsajko added design Design of APIs or of the language itself iteration Involves iteration or the iteration protocol feature Indicates new feature / enhancement requests labels Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself feature Indicates new feature / enhancement requests iteration Involves iteration or the iteration protocol
Projects
None yet
Development

No branches or pull requests

2 participants