# \`isempty(enumerate(Iterators.Stateful(...)))\` advances the stateful iterator?

**URL:** https://discourse.julialang.org/t/isempty-enumerate-iterators-stateful-advances-the-stateful-iterator/71475
**Category:** General Usage
**Created:** [November 15, 2021, 2:18am UTC](https://discourse.julialang.org/t/isempty-enumerate-iterators-stateful-advances-the-stateful-iterator/71475 "2021-11-15T02:18:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bzinberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bzinberg/32/30798_2.png) [@bzinberg](https://discourse.julialang.org/u/bzinberg)
#### Post date: [November 15, 2021, 2:18am UTC](https://discourse.julialang.org/t/isempty-enumerate-iterators-stateful-advances-the-stateful-iterator/71475/1 "2021-11-15T02:18:58Z")

</div>

On Julia 1.6.2:

```julia
itr = enumerate(Iterators.Stateful([:a, :b, :c]))
isempty(itr) && println("It's empty")
isempty(itr) && println("It's empty")
isempty(itr) && println("It's empty")
isempty(itr) && println("It's empty")

```

```julia
It's empty

```

By contrast, if we remove the enumerate, the above prints nothing and evaluates to `false`. Is this intended behavior or is it a bug?

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [November 15, 2021, 4:31am UTC](https://discourse.julialang.org/t/isempty-enumerate-iterators-stateful-advances-the-stateful-iterator/71475/2 "2021-11-15T04:31:07Z")

</div>

Well, stateful iterators are a bit of an underspecified part of the iteration protocol (along with a few other issues), but rather than asking whether this is a bug, I think the better question is whether this can be fixed (yes) and whether this should be fixed (sure, why not). The relevant code is here:  
[https://github.com/JuliaLang/julia/blob/d89addd21b97865eb5445f0051e5946d13842c68/base/essentials.jl#L788](https://github.com/JuliaLang/julia/blob/d89addd21b97865eb5445f0051e5946d13842c68/base/essentials.jl#L788)

In particular, `isdone` needs to be implemented for the enumerate iterator and just pass through to the iterator it wraps. Should be a quick PR for someone who wants to send one in :).

---

<div class="post-metadata">

### Author: ![bzinberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bzinberg/32/30798_2.png) [@bzinberg](https://discourse.julialang.org/u/bzinberg)
#### Post date: [November 16, 2021, 3:44am UTC](https://discourse.julialang.org/t/isempty-enumerate-iterators-stateful-advances-the-stateful-iterator/71475/3 "2021-11-16T03:44:28Z")

</div>

Awesome, thanks @Keno for looking into this!

> rather than asking whether this is a bug, I think the better question is whether this can be fixed (yes) and whether this should be fixed (sure, why not).

FWIW, I would not be able to ask (let alone answer) whether something can or should be fixed without first knowing whether it’s a bug, which is nontrivial because it requires knowing what the intended behavior is. Hence my asking whether that was the intended behavior – it seemed quite possible that someone would come back with “It sure is, here’s why.”

Much appreciate the link to the relevant code. And I see in the [docstring](https://github.com/JuliaLang/julia/blob/d89addd21b97865eb5445f0051e5946d13842c68/base/essentials.jl#L885-L896) of `isdone` they do say:

> This function provides a fast-path hint for iterator completion. This is  
> useful for mutable iterators that want to avoid having elements consumed, if  
> they are not going to be exposed to the user (e.g. to check for done-ness in  
> `isempty` or `zip`). Mutable iterators that want to opt into this feature  
> should define an isdone method that returns true/false depending on whether  
> the iterator is done or not. Stateless iterators need not implement this  
> function. If the result is `missing`, callers may go ahead and compute  
> `iterate(x, state...) === nothing` to compute a definite answer.

i.e., just the info I needed as the implementer of a custom iterator 🙂 So I guess the bug is that information, which is important for someone who wants to implement a mutable custom iterator, is in the docstring for a non-exported function and doesn’t appear on the Julia documentation site. It should probably be at least mentioned on the [Iteration interface](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) page. _That_ I feel I know enough to submit a fix for 🙂

---

<div class="post-metadata">

### Author: ![bzinberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bzinberg/32/30798_2.png) [@bzinberg](https://discourse.julialang.org/u/bzinberg)
#### Post date: [November 16, 2021, 3:51am UTC](https://discourse.julialang.org/t/isempty-enumerate-iterators-stateful-advances-the-stateful-iterator/71475/4 "2021-11-16T03:51:02Z")

</div>

Filed [https://github.com/JuliaLang/julia/pull/43099](https://github.com/JuliaLang/julia/pull/43099).
