# Is there a way to "forward" getindex for Tuples in a type-stable way?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889>\
**Category:** General Usage\
**Created:** [March 26, 2017, 10:48pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889 "2017-03-26T22:48:24Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 27, 2017, 10:10am UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/2 "2017-03-27T10:10:59Z")

</div>

Don’t use `getindex` with runtime integers `i` (`x[i]`) if you want it to be type-stable. Instead, you need all of the those integers to be known at compile-time. So

```julia
for i = 1:3
  x[i]
end

```

are not type stable calls for an inhomogenous tuple, while

```julia
x[1]; x[2]; x[3]

```

is. Two ways to handle this. One is a metaprogramming: generated functions work nicely. But another way is recursion. I’m thinking of writing a blog post to describe it in full, but an example of how to do this is found here:

[https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/callbacks.jl#L195](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/callbacks.jl#L195)

In this example, `apply_discrete_callback!(integrator,callback)` needs to be called on every callback in a tuple (and each callback is differently typed). So the basic call is:

```julia
apply_discrete_callback!(integrator,discrete_callbacks...)

```

This then gets “caught” by function headers like

```julia
apply_discrete_callback!(integrator::ODEIntegrator,callback::DiscreteCallback,args...)

```

where `callback` is the first callback in the list and `args` is the rest. You can then recrusively do something on `callback` and pass `args` into `apply_discrete_callback!`, and it’ll get smaller an smaller. Manage the Base cases and you’re good: no `getindex` was ever needed.

Then, for this code, the compiler will inline this into one giant call. It essentially makes it amount to:

```julia
apply_discrete_callback!(integrator,callback[1])
apply_discrete_callback!(integrator,callback[2])
apply_discrete_callback!(integrator,callback[3])
...

```

knowing what the index is at compile time and thus able to infer each call and be type-stable. The only cost here is the compile time, but if you’re keeping the tuples around and repeatedly calling this, it’ll only have to compile this full recursive routine once for the the tuple.

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889)._
