# Trying to understand how getindex works with vectors

**URL:** <https://discourse.julialang.org/t/trying-to-understand-how-getindex-works-with-vectors/47391>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 28, 2020, 5:36am UTC](https://discourse.julialang.org/t/trying-to-understand-how-getindex-works-with-vectors/47391 "2020-09-28T05:36:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 28, 2020, 5:36am UTC](https://discourse.julialang.org/t/trying-to-understand-how-getindex-works-with-vectors/47391/1 "2020-09-28T05:36:52Z")

</div>

I am trying to track down the functions that are being called when I’m evaluating

```julia
julia> A = 1:4
1:4

julia> A[A .> 2]
2-element Array{Int64,1}:
 3
 4

```

This starts with

```julia
function getindex(A::AbstractArray, I...)
    @_propagate_inbounds_meta
    error_if_canonical_getindex(IndexStyle(A), A, I...)
    _getindex(IndexStyle(A), A, to_indices(A, I)...)
end

```

so I call `Base._getindex` and find

```julia
julia> I = (A .> 2,);

julia> Base._getindex(IndexStyle(A), A, to_indices(A, I)...)
2-element Array{Int64,1}:
 3
 4

```

so far so good. Now I go one level deeper.

```julia
@inline function _getindex(l::IndexStyle, A::AbstractArray, I::Union{Real, AbstractArray}...)
    @boundscheck checkbounds(A, I...)
    return _unsafe_getindex(l, _maybe_reshape(l, A, I...), I...)
end

```

so I try calling `_unsafe_getindex`.

```julia
julia> l = IndexStyle(A)
IndexLinear()

julia> Base._unsafe_getindex(l, Base._maybe_reshape(l, A, I...), I...)
4-element Array{Int64,1}:
 0
 0
 1
 1

```

Eh? What happened here? Why does this not match the value returned by `_getindex`?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 28, 2020, 5:44am UTC](https://discourse.julialang.org/t/trying-to-understand-how-getindex-works-with-vectors/47391/2 "2020-09-28T05:44:27Z")

</div>

You’re missing

```julia
to_indices(A, I)

```

between `_getindex` and and `_unsafe_get_index` (you included it between `getindex` and `_getindex`)
