# How to Make Tuple of Tuple generation Type Stable

**URL:** <https://discourse.julialang.org/t/how-to-make-tuple-of-tuple-generation-type-stable/103842>\
**Category:** Performance\
**Tags:** question\
**Created:** [September 14, 2023, 9:10am UTC](https://discourse.julialang.org/t/how-to-make-tuple-of-tuple-generation-type-stable/103842 "2023-09-14T09:10:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderNenninger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexandernenninger/32/24602_2.png) [@AlexanderNenninger](https://discourse.julialang.org/u/AlexanderNenninger)\
**Post date:** [September 14, 2023, 9:10am UTC](https://discourse.julialang.org/t/how-to-make-tuple-of-tuple-generation-type-stable/103842/1 "2023-09-14T09:10:18Z")

</div>

I have code that needs to generate lists of indices. Cutting away all the fuzz, it boils down to

```julia
using JET

function foobar(K::Int, t::NTuple{N,T}) where {N,T}
    return [t[1:K]]
end

function foo(::Val{N}, k::Int) where {N}
    t = ntuple(i -> rand(Int) + k, N)
    ntuple(i -> foobar(i, t), N)
end

foo(Val(10), 3)
rep = @report_opt foo(Val(10), 3)
print(rep)

```

#### Output

> ═════ 1 possible error found ═════  
> ┌ foo(::Val{10}, k::Int64) @ Main /Users/NENNIA/.julia/dev/TTApproximations/play.jl:54  
> │┌ ntuple(f::var"#62#64"{NTuple{10, Int64}}, n::Int64) @ Base ./ntuple.jl:19  
> ││┌ (::var"#62#64"{NTuple{10, Int64}})(i::Int64) @ Main /Users/NENNIA/.julia/dev/TTApproximations/play.jl:54  
> │││┌ foobar(K::Int64, t::NTuple{10, Int64}) @ Main /Users/NENNIA/.julia/dev/TTApproximations/play.jl:49  
> ││││ runtime dispatch detected: Base.vect(%189::Tuple{Vararg{Int64}})::Vector  
> │││└────────────────────

Calculating by hand, the return type of `foo` should be `Tuple{Vec{NTuple{1, Int}}, Vec{NTuple{2, Int}}, ..., Vec{NTuple{N, Int}}}`.

**How do I make Julia understand that?**

---

<div class="post-metadata">

**Author:** ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)\
**Post date:** [September 14, 2023, 10:33am UTC](https://discourse.julialang.org/t/how-to-make-tuple-of-tuple-generation-type-stable/103842/2 "2023-09-14T10:33:24Z")

</div>

Here you go:

```julia
@inline function foobar(K::Int, t::NTuple{N,T}) where {N,T}
    return [t[1:K]]
end

function foo(::Val{N}, k::Int) where {N}
    t = ntuple(i -> rand(Int) + k, N)
    ntuple((@inline function(i); return foobar(i, t); end), N)
end

```

The difference is that I’ve forced inlining of all functions.
