julia icon indicating copy to clipboard operation
julia copied to clipboard

Disallow construction of NTuple from > N elements

Open jakobnissen opened this issue 4 years ago • 2 comments

Currently, you can construct NTuples for any iterable with more than N elements:

julia> NTuple{1}("hello")
('h',)

julia> NTuple{3}(1:5)
(1, 2, 3)

This strikes me as problematic, and an easy way of getting into trouble. If the user tries to create NTuple from something with more than N elements, they probably made a mistake. It would be much nicer if it behaved like:

julia> NTuple{3}(1:5)
ERROR: ArgumentError: too many elements for tuple type Tuple{T} where T
Stacktrace:
 [1] _totuple_err(T::Any)
   @ Base ./tuple.jl:309

(or similar). In practice, this could be done by simply verifying that the iterator returns nothing before returning the tuple.

This change is breaking, but it may be worth considering for a future, breaking release, but perhaps the issue can be solved in a non-breaking way somehow.

jakobnissen avatar Apr 15 '21 14:04 jakobnissen

Related: https://github.com/JuliaLang/julia/issues/37132

goretkin avatar Apr 15 '21 16:04 goretkin

This is only "breaking" in the sense that fixing it could break some user code that depends on the current, buggy, behavior; however the buggy behavior was never documented. So I think the buggy behavior is not supported and I don't think that fixing it should necessarily be considered as breaking. Does Julia have some backwards compatibility promise document? Perhaps such a document would have answers to questions like this one, if it exists?

nsajko avatar Jan 18 '24 19:01 nsajko

essentially a duplicate of https://github.com/JuliaLang/julia/issues/18312, although they examine different consequences so maybe both should be left open

adienes avatar Jul 30 '25 14:07 adienes