# NTuple{N}(v) silently truncates when length(v) \> N

**URL:** <https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959>\
**Category:** General Usage\
**Tags:** question, ntuple\
**Created:** [January 18, 2024, 1:47pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959 "2024-01-18T13:47:04Z")\
**Posts on this page:** 9\
**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 18, 2024, 1:47pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/1 "2024-01-18T13:47:04Z")

</div>

MWE:

```julia
julia> NTuple{3}(zeros(4))
(0.0, 0.0, 0.0)

```

I expected an error, am I missing something?

(on Julia 1.10, master, 1.9, etc, this is not new)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 18, 2024, 2:00pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/2 "2024-01-18T14:00:58Z")

</div>

The implementation of `_totuple` in tuple.jl accepts iterables with excess elements but errors on insufficient elements.

---

<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 18, 2024, 2:06pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/3 "2024-01-18T14:06:31Z")

</div>

What I am unsure about if this is intended, or a bug.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 18, 2024, 2:10pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/4 "2024-01-18T14:10:00Z")

</div>

Seems intentional because of the use of `Iterators.take` in one of the methods.

---

<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 18, 2024, 3:29pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/5 "2024-01-18T15:29:26Z")

</div>

I don’t think it is, but it can’t hurt to ask, so

> <https://github.com/JuliaLang/julia/issues/52963>
>
> \`\`\`julia
> julia\> NTuple{3}(zeros(4))
> (0.0, 0.0, 0.0)
> \`\`\`
> 
> Is this intentiona…l? I expected an error message. Cf eg
> 
> \`\`\`julia
> julia\> NTuple{3}((1,2,3,4))
> ERROR: MethodError: Cannot \`convert\` an object of type 
> NTuple{4, Int64} to an object of type 
> Tuple{T, T, T} where T
> \`\`\`
> 
> (tested on 1.10 and master, but this is not a regression, also the same in previous versions I checked)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 18, 2024, 3:42pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/6 "2024-01-18T15:42:07Z")

</div>

I doubt it’ll change anything because you’re supposed to be able to make tuples from iterables that don’t even have a `length`.

```julia
julia> NTuple{7}(Iterators.cycle(1:3))
(1, 2, 3, 1, 2, 3, 1)

julia> length(Iterators.cycle(1:3))
ERROR: MethodError: no method matching length(::Base.Iterators.Cycle{UnitRange{Int64}})

```

---

<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 18, 2024, 3:45pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/7 "2024-01-18T15:45:40Z")

</div>

We shall see. I think that silently applying `Iterators.take` on inputs is a questionable decision. I can live with it, but it should at least be documented.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 18, 2024, 4:03pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/8 "2024-01-18T16:03:17Z")

</div>

It _is_ documented that “A tuple can be constructed from an iterator by using a `Tuple` type as constructor”, and the iteration interface is pretty clear that an iterable does not always have a fixed length. If a tuple can be constructed from an iterable with unfixed length, it’s not reasonable to error on iterables with a greater fixed length; both kinds of iterables are “silently truncated” the same way. `_totuple` does work on longer tuples, so an input tuple dispatches `Tuple` constructors to a separate method that may only `convert`. Maybe this exception should be documented.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 18, 2024, 6:16pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959/9 "2024-01-18T18:16:23Z")

</div>

[here](https://github.com/JuliaLang/julia/issues/20499)a discussion that seems relevant to me
