You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
Currently, Julia generally assumes iterators are stateless. This is clearly wrong, and so leads to issues:
isempty(::EachLine)
expends a line #27412Iterators.takewhile(predicate, itr::Stateful)
causes the first non-matching element to be dropped from the Stateful Iterator #48195I 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:
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 implementIsStateful
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, unlessisdone
has been defined.@oxinabox proposed something similar in #27412.
The text was updated successfully, but these errors were encountered: