# 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:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [September 19, 2017, 2:23am UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/21 "2017-09-19T02:23:20Z")

</div>

One other option is to add a layer of indirection so that all the bt[i]'s of a particular type are indexed by subarrays of another array; each subarray is parameterized by this type. Then the code can run through the `bt[i]` entries ordered by type rather than by `i`, so that the inner loops are all type-stable. I think that `DifferentialEquations.jl` uses a related technique for specifying boundary conditions. (The situation with boundary conditions is more complicated because these are functions rather than quantities, and each function is its own type in Julia.)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 19, 2017, 1:39pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/22 "2017-09-19T13:39:51Z")

</div>

As far as I understand, that’s what TypeSortedCollections.jl does for you automatically.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 19, 2017, 11:44pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/23 "2017-09-19T23:44:49Z")

</div>

Yeah I was looking at `TypeSortedCollections.jl` but something from their README scared me a bit:

> TypeSortedCollections are appropriate when the number of different types in a heterogeneous collection is (much) smaller than the number of elements of the collection. If the number of types is approximately the same as the number of elements, a plain Tuple may be a better choice.

What I will soon-ish do is simply make my container a tuple and benchmark everything, versus it being a simple tuple or a `TypeSortedCollection`. I will definitely post my results here!

---

<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:** [September 20, 2017, 1:00am UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/24 "2017-09-20T01:00:01Z")

</div>

Awesome, would be interesting to see.

I added that comment on account of the fact that if the number of types is close to the number of elements, you’re just going to have a bunch of extra for loops over 1-element `Vector`s stored in the `TypeSortedCollection`. So in this case, using a `TypeSortedCollection` instead of a `Tuple` will just add a bit of overhead without getting any payback in terms of reduced compilation time.

---

<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:** [September 20, 2017, 5:01am UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/25 "2017-09-20T05:01:28Z")

</div>

FYI, I just added `broadcast!` (with `AbstractVector` destination) to TypeSortedCollections (requires TypeSortedCollections master for now):

```julia
julia> using TypeSortedCollections, BenchmarkTools

julia> x = 4;

julia> ys = Number[1.; 2; 3];

julia> sortedys = TypeSortedCollection(ys);

julia> results = similar(ys, Float64);

julia> results .= x .* sortedys
3-element Array{Float64,1}:
  4.0
  8.0
 12.0

julia> @benchmark $results .= $x .* $sortedys # with julia -O3 --check-bounds=no

BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 10.196 ns (0.00% GC)
  median time: 10.836 ns (0.00% GC)
  mean time: 11.453 ns (0.00% GC)
  maximum time: 1.048 μs (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 999

```

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [December 6, 2017, 5:44pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889/26 "2017-12-06T17:44:38Z")

</div>

Fyi, the exact example I gave in my original question is now type-stable thanks to the magic of [https://github.com/JuliaLang/julia/pull/24362](https://github.com/JuliaLang/julia/pull/24362) which just recently got merged into master 🙂

[Previous page](https://discourse.julialang.org/t/is-there-a-way-to-forward-getindex-for-tuples-in-a-type-stable-way/2889.md?page=1)
