# Cryptic error message in constructing an NTuple

**URL:** <https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149>\
**Category:** General Usage\
**Tags:** question, ntuple\
**Created:** [June 27, 2020, 9:45am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149 "2020-06-27T09:45:44Z")\
**Posts on this page:** 5\
**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:** [June 27, 2020, 9:45am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149/1 "2020-06-27T09:45:44Z")

</div>

I’m on Julia 1.4.2.

This works

```julia
julia> NTuple{1}(3)
(3,)

```

This doesn’t, and doesn’t say why

```julia

julia> NTuple(3)
ERROR: UndefVarError: E not defined
Stacktrace:
 [1] _totuple(::Type{Tuple{Vararg{T,N}} where T where N}, ::Int64) at ./tuple.jl:256
 [2] Tuple{Vararg{T,N}} where T where N(::Int64) at ./tuple.jl:230
 [3] top-level scope at REPL[3]:1

```

Is this not a reasonable way to construct `NTuples`? In any case the error message might need to be improved if this isn’t how it should be done. What’s bad here is that `E` doesn’t appear in the stacktrace, so it’s impossible to infer what’s going on without digging into the source.

---

<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:** [June 27, 2020, 10:14am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149/2 "2020-06-27T10:14:13Z")

</div>

Try

```julia
julia> NTuple((3,))
(3,)

```

Incidentally, the fact that `NTuple{1}(3)` works comes from numbers being iterable. Eg `NTuple{1}(:foo)` would fail, so it is not a good approach to rely on in generic code.

Finally, I am not sure what you are trying to accomplish here — a single-element tuple is an `NTuple` by construction, so this is a no-op.

---

<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:** [June 27, 2020, 10:23am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149/3 "2020-06-27T10:23:08Z")

</div>

I’m not focused on a single element, this was just an example, although I suppose my attempt at simplifying it has changed the context. I was originally trying to construct an `NTuple` by iterating over a generator, and not from a `Tuple`. I’m not sure if this is legitimate usage, but I was trying out something along the lines of

```julia
julia> Tuple(i for i=1:3)
(1, 2, 3)

julia> NTuple(i for i=1:3)
ERROR: UndefVarError: E not defined

```

---

<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:** [June 27, 2020, 10:33am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149/4 "2020-06-27T10:33:42Z")

</div>

The error message is improved on master:

```julia
julia> NTuple(i for i=1:3)
ERROR: ArgumentError: too few elements for tuple type Tuple{Vararg{T,N}} where T where N

```

That said, why not just use

```julia
julia> Tuple(i for i in 1:3)
(1, 2, 3)

```

or

```julia
julia> NTuple{3}(i for i in 1:3)
(1, 2, 3)

```

depending on whether you know the information at compile time?

---

<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:** [June 27, 2020, 11:16am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149/5 "2020-06-27T11:16:41Z")

</div>

Thanks, the new error message is significantly better.

As for why I used `NTuple` instead of `Tuple`: no specific reason, I was just trying things out to see what works and what doesn’t
