# Empty range has first element, bug or feature?

**URL:** <https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236>\
**Category:** General Usage\
**Created:** [October 20, 2023, 12:56pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236 "2023-10-20T12:56:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 20, 2023, 12:56pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/1 "2023-10-20T12:56:42Z")

</div>

I found the following behavoir, which is IMO a bug, but not sure:

```julia

julia> isempty(1:0)
true

julia> first(1:0)
1

```

I would have expected an error.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 20, 2023, 1:00pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/2 "2023-10-20T13:00:00Z")

</div>

yeah, that’s a bug. (file an issue)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [October 20, 2023, 1:06pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/3 "2023-10-20T13:06:07Z")

</div>

Is it a bug? Ranges also have a `last`, `step`, and `length`.

```julia
julia> r = 1:0
1:0

julia> first(r)
1

julia> last(r)
0

julia> step(r)
1

julia> length(r)
0

```

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [October 20, 2023, 1:16pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/4 "2023-10-20T13:16:17Z")

</div>

Semantically it sounds like a bug. An empty collection cannot not have a first or last element I think.  
`length` being equal to zero and `step` being allowed to have some value sound reasonable though.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 20, 2023, 1:16pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/5 "2023-10-20T13:16:20Z")

</div>

> [@mkitti](#):
>
> Is it a bug?

I think it is given:

```julia
julia> a = 1:0
1:0

julia> collect(a)
Int64[]

julia> first(collect(a))
ERROR: BoundsError: attempt to access 0-element Vector{Int64} at index [1]

```

having `step` is fine though.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 20, 2023, 1:19pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/6 "2023-10-20T13:19:36Z")

</div>

Turns out its a feature, not a bug: [Empty range has first element · Issue #51793 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/51793#issuecomment-1772713011)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 20, 2023, 2:57pm UTC](https://discourse.julialang.org/t/empty-range-has-first-element-bug-or-feature/105236/7 "2023-10-20T14:57:43Z")

</div>

It’s pretty important because it allows you to return useful information from searches where the result is an empty range, because the start of the range tells you the location. For example, searching for word boundaries returns an empty range that has a location:

```julia
julia> findnext(r"\b", "foo bar", 2)
4:3

```
