Skip to content

Commit

Permalink
ntuple(f, -1) throws ArgumentError
Browse files Browse the repository at this point in the history
  • Loading branch information
alyst committed May 5, 2017
1 parent 78e835e commit 2c7dc16
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Breaking changes

This section lists changes that do not have deprecation warnings.

* `ntuple(f, n::Integer)` throws `ArgumentError` if `n` is negative.
Previously an empty tuple was returned ([#21697]).

Library improvements
--------------------

Expand Down
3 changes: 2 additions & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ julia> ntuple(i -> 2*i, 4)
```
"""
function ntuple(f::F, n::Integer) where F
t = n <= 0 ? () :
(n >= 0) || throw(ArgumentError(string("tuple length should be ≥0, got ", n)))
t = n == 0 ? () :
n == 1 ? (f(1),) :
n == 2 ? (f(1), f(2)) :
n == 3 ? (f(1), f(2), f(3)) :
Expand Down
2 changes: 2 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ for n = 0:20
@test t[i] == i
end
end
# issue #21697
@test_throws ArgumentError ntuple(identity, -1)

# issue #19719
@test_throws BoundsError (1,2,3)[falses(4)]
Expand Down

0 comments on commit 2c7dc16

Please sign in to comment.