# Type inference when just making a tuple

**URL:** <https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071>\
**Category:** Performance\
**Tags:** tuple, code\_warntype, type-stability\
**Created:** [June 1, 2024, 11:24pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071 "2024-06-01T23:24:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [June 1, 2024, 11:24pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/1 "2024-06-01T23:24:36Z")

</div>

Hello,

I’m seeing some type instabilities in my code.

I saw that converting a vector into a tuple generates instabilities. Here a very simple example

```julia
using Test

function prova(vec)
    return Tuple(vec)
end

T = Float64
a = rand(T, 3)

@inferred prova(a)

```

```julia
ERROR: return type Tuple{Float64, Float64, Float64} does not match inferred return type Tuple{Vararg{Float64}}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] top-level scope
   @ Untitled-1:35

```

It is surprising how such a simple code can generate instabilities.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [June 1, 2024, 11:29pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/2 "2024-06-01T23:29:30Z")

</div>

It doesn’t know the length of `vec`, so it’s not too surprising - that’s where the `Vararg` comes from.

---

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [June 1, 2024, 11:31pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/3 "2024-06-01T23:31:01Z")

</div>

Ok, makes sense. But how to solve this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 2, 2024, 12:06am UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/4 "2024-06-02T00:06:39Z")

</div>

Use StaticArrays.jl depending on the problem size

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 2, 2024, 2:24pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/5 "2024-06-02T14:24:41Z")

</div>

Note that if your static arrays vary in size you’ll run into the exact same issue. Perhaps the better question is _why_ you want to convert vectors into tuples?

---

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [June 2, 2024, 8:47pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/6 "2024-06-02T20:47:28Z")

</div>

The reason was to have better performances. But maybe vectors are more suitable for my case.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [June 2, 2024, 9:16pm UTC](https://discourse.julialang.org/t/type-inference-when-just-making-a-tuple/115071/7 "2024-06-02T21:16:42Z")

</div>

The main reason Tuples have better performance than Vectors is that the compiler knows their length, so they can be stored on the stack (not heap allocated). If your data has variable length, then Julia needs to compile a new function for every unique length. In this case, a vector is almost always the right choice.
