Skip to content

Disallow construction of NTuple from > N elements #40495

Open
@jakobnissen

Description

@jakobnissen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    collectionsData structures holding multiple items, e.g. setsneeds decisionA decision on this change is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions