# Newly defined getindex() behaves differently in REPL and a function

**URL:** <https://discourse.julialang.org/t/newly-defined-getindex-behaves-differently-in-repl-and-a-function/2225>\
**Category:** General Usage\
**Created:** [February 22, 2017, 12:03am UTC](https://discourse.julialang.org/t/newly-defined-getindex-behaves-differently-in-repl-and-a-function/2225 "2017-02-22T00:03:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [February 22, 2017, 12:03am UTC](https://discourse.julialang.org/t/newly-defined-getindex-behaves-differently-in-repl-and-a-function/2225/1 "2017-02-22T00:03:06Z")

</div>

I need to deal with tuples of tuples a lot. They are specifically of type `NTuple{3,NTuple{2,Int}}`; in other words, each inner tuple has two integers and the outer tuple has three such tuples, like `t = ((3,4), (5,6), (7,8))`.

Also, I frequently need to construct a length-3 tuple by taking one integer per inner tuple. For example, from the above `t` I construct `(4,5,8)` by taking the 2nd entry from the first inner tuple, 1st entry from the second inner tuple, and 2nd entry from the third inner tuple.

Because I perform this operation frequently, I defined a new `getindex` function as a convenience function. The goal was to perform the above operation by `t[2,1,2]`. The new `getindex` implementation is as follows, and it seemed to work well in REPL:

```julia
julia> Base.getindex(t::NTuple{3,NTuple{2,Int}}, i::Int, j::Int, k::Int) = (t[1][i], t[2][j], t[3][k])

julia> t = ((3,4), (5,6), (7,8))
((3,4),(5,6),(7,8))

julia> t[2,1,2]
(4,5,8)

```

However, if I use this new `getindex` inside some function, it suddenly starts returning only the first two entries of the constructed length-3 tuple:

```julia
julia> test(t) = t[2,1,2]
test (generic function with 1 method)

julia> test(t)
(4,5)

```

Am I doing something wrong? Should I file an issue for this? This happens both in Julia v0.5 and v0.6.

---

<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:** [February 22, 2017, 2:23am UTC](https://discourse.julialang.org/t/newly-defined-getindex-behaves-differently-in-repl-and-a-function/2225/2 "2017-02-22T02:23:29Z")

</div>

> [@wsshin](#):
>
> Am I doing something wrong? Should I file an issue for this? This happens both in Julia v0.5 and v0.6.

I can reproduce your problem. At first glance, it looks like a bug.

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [February 22, 2017, 2:45am UTC](https://discourse.julialang.org/t/newly-defined-getindex-behaves-differently-in-repl-and-a-function/2225/3 "2017-02-22T02:45:57Z")

</div>

Thanks for the confirmation! I filed an issue: [https://github.com/JuliaLang/julia/issues/20733](https://github.com/JuliaLang/julia/issues/20733).

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [February 22, 2017, 2:05pm UTC](https://discourse.julialang.org/t/newly-defined-getindex-behaves-differently-in-repl-and-a-function/2225/4 "2017-02-22T14:05:00Z")

</div>

Running `test(t,i,j,k) = Base.getindex(t,i,j,k)` (after the above) also yields a function that returns a length-2 tuple. Comparing the two methods using `code_llvm` shows that the lines that generate the third element of the tuple in `Base.getindex` are gone in `test`.

A simple work-around seems to be to wrap the Tuple in your own type. (Maybe this eliminates some optimizations that are specific to the `Tuple` type?) For example, with `t` and `test` defined as above:

```julia
julia> type mytype
         x::typeof(t)
       end

julia> my_t = mytype(t)
mytype(((3, 4), (5, 6), (7, 8)))

julia> Base.getindex(t::mytype, i::Int, j::Int, k::Int) = (t.x[1][i], t.x[2][j], t.x[3][k]);

julia> my_t[2,1,2]
(4, 5, 8)

julia> test(my_t)
(4, 5, 8)

```
