# Inconsistent behavior of getindex method with LinearIndices

**URL:** https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041
**Category:** General Usage
**Tags:** indexing
**Created:** [November 1, 2018, 8:25am UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041 "2018-11-01T08:25:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [November 1, 2018, 8:25am UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041/1 "2018-11-01T08:25:48Z")

</div>

Currently we have two `getindex` methods with `LinearIndices` [here](https://github.com/JuliaLang/julia/blob/master/base/indices.jl#L409_L418)

```julia
function getindex(iter::LinearIndices, i::Int)
    @_inline_meta
    @boundscheck checkbounds(iter, i)
    i
end
function getindex(iter::LinearIndices, i::AbstractRange{<:Integer})
    @_inline_meta
    @boundscheck checkbounds(iter, i)
    @inbounds isa(iter, LinearIndices{1}) ? iter.indices[1][i] : (first(iter):last(iter))[i]
end

```

It’s quite strange that the first one just return `i`. I suppose that it should return the `i`th element like the second method. Let’s see the example below:

```julia
julia> iter = LinearIndices((-1:1,))
3-element LinearIndices{1,Tuple{UnitRange{Int64}}}:
 1
 2
 3

julia> iter[2:2]
0:0

julia> iter[2]
2

```

---

<div class="post-metadata">

### Author: ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)
#### Post date: [November 1, 2018, 8:03pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041/2 "2018-11-01T20:03:00Z")

</div>

```julia
julia> iter[2,:,]
 2

```

---

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [November 1, 2018, 11:44pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041/3 "2018-11-01T23:44:57Z")

</div>

Yeah, I know, this will fallback to `AbstractArray`’s default `getindex` implementation. My key point here is why `LinearIndices` here choose to not support negative `UnitRange`.

Seems `CartesianIndices` supports it well:

```julia
julia> inds = CartesianIndices((-1:1, -1:1))
3×3 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}:
 CartesianIndex(-1, -1) CartesianIndex(-1, 0) CartesianIndex(-1, 1)
 CartesianIndex(0, -1) CartesianIndex(0, 0) CartesianIndex(0, 1) 
 CartesianIndex(1, -1) CartesianIndex(1, 0) CartesianIndex(1, 1) 

julia> inds[1]
CartesianIndex(-1, -1)

julia> iter = LinearIndices((-1:1, -1:1))
3×3 LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}:
 1 4 7
 2 5 8
 3 6 9

julia> iter[CartesianIndex(-1,-1)]
ERROR: BoundsError: attempt to access 3×3 LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}} at index [-1, -1]
Stacktrace:
 [1] throw_boundserror(::LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}, ::Tuple{Int64,Int64}) at .\abstractarray.jl:484
 [2] checkbounds at .\abstractarray.jl:449 [inlined]
 [3] _getindex at .\abstractarray.jl:927 [inlined]
 [4] getindex(::LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}, ::CartesianIndex{2}) at .\abstractarray.jl:905
 [5] top-level scope at none:0

```

If the aim of `LinearIndices` and `CartesianIndices` is to provide conversion between each other, then I suppose this error should not happen(the last one is supposed to return `1`, I guess?).

---

<div class="post-metadata">

### Author: ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)
#### Post date: [November 2, 2018, 4:53am UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041/4 "2018-11-02T04:53:58Z")

</div>

```julia
julia> inds[CartesianIndex(-1, 1)]
ERROR: BoundsError: attempt to access 3×3 
CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}
 at index [-1, 1]

```

---

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [November 2, 2018, 7:18am UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-getindex-method-with-linearindices/17041/5 "2018-11-02T07:18:46Z")

</div>

Thanks!

It’s my fault.
