# \[Julia 1.0.2\] indexing into UnitRange buggy? (1:4)\[6:4\] == 6:5

**URL:** <https://discourse.julialang.org/t/julia-1-0-2-indexing-into-unitrange-buggy-1-4-6-4-6-5/22661>\
**Category:** General Usage\
**Created:** [April 2, 2019, 11:43pm UTC](https://discourse.julialang.org/t/julia-1-0-2-indexing-into-unitrange-buggy-1-4-6-4-6-5/22661 "2019-04-02T23:43:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [April 2, 2019, 11:43pm UTC](https://discourse.julialang.org/t/julia-1-0-2-indexing-into-unitrange-buggy-1-4-6-4-6-5/22661/1 "2019-04-02T23:43:33Z")

</div>

I was trying to index using a UnitRange like `list[2:end]` then was curious what gonna be happen when my List only has length 1. Thus I began testing with a UnitRange as list which led me to the following strange behaviour.  
Version 1.0.2

```julia
julia> (1:4)[2:1]
2:1

julia> (1:4)[5:4]
5:4

julia> (1:4)[6:4]
6:5

julia> (1:4)[6:4]
6:5

julia> (1:4)[7:4]
7:6

julia> (1:4)[1:4]
1:4

julia> (1:4)[5:4]
5:4

julia> (1:4)[4:4]
4:4

julia> (1:4)[4]
4

julia> (1:4)[5]
ERROR: BoundsError: attempt to access 4-element UnitRange{Int64} at index [5]
Stacktrace:
 [1] throw_boundserror(::UnitRange{Int64}, ::Int64) at .\abstractarray.jl:484
 [2] getindex(::UnitRange{Int64}, ::Int64) at .\range.jl:597
 [3] top-level scope at none:0

julia> (1:4)[end]
4

julia> (1:4)[5:end]
5:4

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 3, 2019, 12:00am UTC](https://discourse.julialang.org/t/julia-1-0-2-indexing-into-unitrange-buggy-1-4-6-4-6-5/22661/2 "2019-04-03T00:00:11Z")

</div>

`n : n - 1` is the canonical representation of an empty `UnitRange`. In the case of `(1:4)[6:4]`, we have `6:4 === 6:5`, and indexing the range `1:4` with an empty range results in an empty range as expected:

```julia
julia> isempty((1:4)[6:4])
true

```

How that empty range is represented (e.g., `6 : 5` or `1 : 0`) shouldn’t really matter and probably just follows from the logic for the general case.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [April 3, 2019, 12:44am UTC](https://discourse.julialang.org/t/julia-1-0-2-indexing-into-unitrange-buggy-1-4-6-4-6-5/22661/3 "2019-04-03T00:44:07Z")

</div>

> [@tkoolen](#):
>
> `n : n - 1` is the canonical representation of an empty `UnitRange` .

This also preserves the nice property of a UnitRange: length = stop - start + 1

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [April 3, 2019, 5:04am UTC](https://discourse.julialang.org/t/julia-1-0-2-indexing-into-unitrange-buggy-1-4-6-4-6-5/22661/4 "2019-04-03T05:04:48Z")

</div>

In other words, `6:4` and `6:5` are the same cos they are both empty, but `6:-1;4` and `6:-1:5` are different.
