# Empty iterator

**URL:** <https://discourse.julialang.org/t/empty-iterator/4440>\
**Category:** General Usage\
**Created:** [June 24, 2017, 9:32am UTC](https://discourse.julialang.org/t/empty-iterator/4440 "2017-06-24T09:32:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [June 24, 2017, 9:32am UTC](https://discourse.julialang.org/t/empty-iterator/4440/1 "2017-06-24T09:32:22Z")

</div>

a function of mine returns an iterator. in some cases, i want to return an empty iterator, but one that is efficient and typed. so i came up with this little thing

```julia
struct Empty{T} end
Base.iteratorsize(::Type{<:Empty}) = Base.HasLength()
Base.eltype(::Type{Empty{T}}) where T = T
Base.start(::Empty) = nothing
Base.done(::Empty, st) = true
Base.length(::Empty) = 0

empty(T) = Empty{T}()

```

but i’m wondering if there is some neat built-in way to do this

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [June 25, 2017, 1:07am UTC](https://discourse.julialang.org/t/empty-iterator/4440/2 "2017-06-25T01:07:56Z")

</div>

Wouldn’t an empty array serve the same purpose? You can construct it with `Array{T}(0)`. But it might have more overhead.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [June 25, 2017, 8:32am UTC](https://discourse.julialang.org/t/empty-iterator/4440/3 "2017-06-25T08:32:41Z")

</div>

array was my first thought, but since Julia is rich in specialized enumerators, such a type seems to make sense.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [June 25, 2017, 12:56pm UTC](https://discourse.julialang.org/t/empty-iterator/4440/4 "2017-06-25T12:56:45Z")

</div>

In a few situations (e.g., tuples or StaticArrays), you can tell via the type system that a container is empty; in such cases, having a special type for empty iteration might make sense.

But in most cases you have to check a _value_ to determine whether the container is empty (e.g., `isempty(c)` which returns a boolean value). In such cases, you probably don’t want to use a distinct type to encode an empty iterator, because that will make your method that returns an iterator non-inferrable. As long as you can write an efficient `done` method for your iterator type, you probably won’t notice the overhead from iterating over empty iterators.

---

<div class="post-metadata">

**Author:** ![Abhinav\_Bhatia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhinav_bhatia/32/18748_2.png) [@Abhinav\_Bhatia](https://discourse.julialang.org/u/Abhinav_Bhatia)\
**Post date:** [November 11, 2021, 10:47pm UTC](https://discourse.julialang.org/t/empty-iterator/4440/5 "2021-11-11T22:47:17Z")

</div>

How about simply `1:0` if the type `T` is `Int`? It has no overhead afaik.
