Skip to content

Commit

Permalink
doc: add warning about stateful iterators to isempty (#43900)
Browse files Browse the repository at this point in the history
The manual now mentions that stateful iterators should implement `isdone`,
so it appears `isempty` is not supposed to consume elements.
This matters a lot for callers as if you can't rely on this you can't do
`isempty(c) && return; for x in c` but instead have to call `iterate(c)` manually.
  • Loading branch information
nalimilan committed Mar 2, 2022
1 parent 593bf93 commit b10aa56
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,13 @@ inferencebarrier(@nospecialize(x)) = Ref{Any}(x)[]
Determine whether a collection is empty (has no elements).
!!! warning
`isempty(itr)` may consume the next element of a stateful iterator `itr`
unless an appropriate `Base.isdone(itr)` or `isempty` method is defined.
Use of `isempty` should therefore be avoided when writing generic
code which should support any iterator type.
# Examples
```jldoctest
julia> isempty([])
Expand Down

0 comments on commit b10aa56

Please sign in to comment.