# Escaping the inference length limit for tuples

**URL:** https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265
**Category:** Internals & Design
**Tags:** question
**Created:** [January 2, 2024, 4:26pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265 "2024-01-02T16:26:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 2, 2024, 4:26pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/1 "2024-01-02T16:26:13Z")

</div>

A lot of functions limit tuple for which they specialize at 32 elements. It is easy to run into non-inferred code because of this, eg

```julia
z = NamedTuple{ntuple(i -> Symbol("a" * string(i)), 33)}(ntuple(i -> (1, 2.0, π, 3f0)[i % 4 + 1], 33))
g(z) = sum(values(z))
@code_warntype g(z)

```

I do understand the reasoning behind this limit (that said, with improvements in compile time and precompilation, I wonder if it could be relaxed now, but is just a side question).

What I am wondering about is the following: assuming that I know what I am doing, and want to use longer (heterogeneous) tuples, and have the code inferred, what is the best option?

Eg I could introduce a wrapper type and reimplement all functions that deal with it using `@generated` etc. But that is quite a bit of work.

---

<div class="post-metadata">

### Author: ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)
#### Post date: [April 10, 2024, 7:15am UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/2 "2024-04-10T07:15:38Z")

</div>

I’m also running into this limit. What did you end up with?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 10, 2024, 7:18am UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/3 "2024-04-10T07:18:09Z")

</div>

Nesting named tuples. Eg instead of

```julia
(β = 1.0, σ_ε = 0.2, σ_η = 0.4)

```

using

```julia
(β = 1.0, noises = (σ_ε = 0.2, σ_η = 0.4))

```

This, incidentally, also lead to better code organization.

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [April 11, 2024, 8:04pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/4 "2024-04-11T20:04:02Z")

</div>

This is such a great hack, I’m taking it.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 12, 2024, 1:12pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/5 "2024-04-12T13:12:57Z")

</div>

Thanks for your kind words, but it is simply the realization of a silly mistake: specifically, if I am naming 32+ heterogeneous things in a flat container, I am doing something wrong. It does not only smell, but it reeks, and instead of trying to work around this at the compiler level I actually benefit from redesigning my code.

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [April 12, 2024, 3:27pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/6 "2024-04-12T15:27:53Z")

</div>

Nah man stick with bad and crazy. I want to see the compiler dispatch on your 7,300 element nested tuples

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [April 12, 2024, 3:48pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/7 "2024-04-12T15:48:52Z")

</div>

Making it nested is just better and cleaner in general, not only for the compiler (:  
So many times I see tables with columns like `flux_a, err_a, flux_b, err_b, flux_c, err_c, ...` – sometimes with 10-20 such columns! Great that in Julia it’s both convenient and performant to work with them more naturally, so the first thing I do is convert these to `flux=(a=x+-y, b=x+-y, ...)`.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [April 12, 2024, 8:17pm UTC](https://discourse.julialang.org/t/escaping-the-inference-length-limit-for-tuples/108265/8 "2024-04-12T20:17:07Z")

</div>

ComponentArrays.jl is also a great way to switch between nested and flat representations on the fly.
