# Why is the NTuple{N} constructor not type stable when constructing a Tuple from a Vector?

**URL:** <https://discourse.julialang.org/t/why-is-the-ntuple-n-constructor-not-type-stable-when-constructing-a-tuple-from-a-vector/54185>\
**Category:** General Usage\
**Tags:** question, tuple, ntuple\
**Created:** [January 29, 2021, 2:11pm UTC](https://discourse.julialang.org/t/why-is-the-ntuple-n-constructor-not-type-stable-when-constructing-a-tuple-from-a-vector/54185 "2021-01-29T14:11:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 29, 2021, 2:11pm UTC](https://discourse.julialang.org/t/why-is-the-ntuple-n-constructor-not-type-stable-when-constructing-a-tuple-from-a-vector/54185/1 "2021-01-29T14:11:39Z")

</div>

```julia
julia> @code_warntype NTuple{3}([1,2,3])
Variables
  #self#::Type{Tuple{T, T, T} where T}
  itr::Vector{Int64}

Body::Tuple{Int64, Int64, Int64, Vararg{Any}}
1 ─ %1 = Base._totuple($(Expr(:static_parameter, 1)), itr)::Tuple{Int64, Int64, Int64, Vararg{Any}}
└── return %1

```

The inferred type is `Tuple{Int64, Int64, Int64, Vararg{Any}}`, but shouldn’t the number of elements be known? Strangely the following is type stable:

```julia
julia> @code_warntype NTuple{3,Int}([1,2,3])
Variables
  #self#::Core.Const(Tuple{Int64, Int64, Int64})
  itr::Vector{Int64}

Body::Tuple{Int64, Int64, Int64}
1 ─ %1 = Base._totuple($(Expr(:static_parameter, 1)), itr)::Tuple{Int64, Int64, Int64}
└── return %1

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 29, 2021, 2:28pm UTC](https://discourse.julialang.org/t/why-is-the-ntuple-n-constructor-not-type-stable-when-constructing-a-tuple-from-a-vector/54185/2 "2021-01-29T14:28:47Z")

</div>

It seems that the code that creates the tuple does not get specialized on the type:

[https://github.com/JuliaLang/julia/blob/750f42aac2ad09b9ccd79408ee4e19941f668fa4/base/tuple.jl#L312-L317](https://github.com/JuliaLang/julia/blob/750f42aac2ad09b9ccd79408ee4e19941f668fa4/base/tuple.jl#L312-L317)

Forcing specialization via:

```julia
@eval Base begin
    function _totuple(::Type{T}, itr, s...) where {T}
        @_inline_meta
        y = iterate(itr, s...)
        y === nothing && _totuple_err(T)
        return (convert(fieldtype(T, 1), y[1]), _totuple(tuple_type_tail(T), itr, y[2])...)
    end
end

```

seems like it makes it type stable. Maybe this is done to not generate an excessive amount of signatures for the method.
