Cryptic error message in constructing an NTuple

I’m on Julia 1.4.2.

This works

julia> NTuple{1}(3)

This doesn’t, and doesn’t say why

julia> NTuple(3)
ERROR: UndefVarError: E not defined
 [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.


julia> NTuple((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.

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> Tuple(i for i=1:3)
(1, 2, 3)

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

The error message is improved on master:

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> Tuple(i for i in 1:3)
(1, 2, 3)


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

depending on whether you know the information at compile time?

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