diff --git a/base/tuple.jl b/base/tuple.jl index 863240034483f..f6a6e6ee130f4 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -123,6 +123,23 @@ end _ntuple(f, n) = (@_noinline_meta; ([f(i) for i = 1:n]...)) # inferrable ntuple +ntuple(f, ::Type{Val{0}}) = (@_inline_meta; ()) +ntuple(f, ::Type{Val{1}}) = (@_inline_meta; (f(1),)) +ntuple(f, ::Type{Val{2}}) = (@_inline_meta; (f(1), f(2))) +ntuple(f, ::Type{Val{3}}) = (@_inline_meta; (f(1), f(2), f(3))) +ntuple(f, ::Type{Val{4}}) = (@_inline_meta; (f(1), f(2), f(3), f(4))) +ntuple(f, ::Type{Val{5}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5))) +ntuple(f, ::Type{Val{6}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6))) +ntuple(f, ::Type{Val{7}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7))) +ntuple(f, ::Type{Val{8}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8))) +ntuple(f, ::Type{Val{9}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9))) +ntuple(f, ::Type{Val{10}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10))) +ntuple(f, ::Type{Val{11}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10), f(11))) +ntuple(f, ::Type{Val{12}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10), f(11), f(12))) +ntuple(f, ::Type{Val{13}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10), f(11), f(12), f(13))) +ntuple(f, ::Type{Val{14}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10), f(11), f(12), f(13), f(14))) +ntuple(f, ::Type{Val{15}}) = (@_inline_meta; (f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10), f(11), f(12), f(13), f(14), f(15))) + function ntuple{F,N}(f::F, ::Type{Val{N}}) Core.typeassert(N, Int) _ntuple((), f, Val{N}) diff --git a/test/tuple.jl b/test/tuple.jl index 7756797e9af4d..ec94dd8e28413 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -251,3 +251,8 @@ end tuple16int = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) @test Tuple16Int(tuple16int) isa Tuple16Int end + +# PR #21446 +for n = 0:15 + @test ntuple(identity, Val{n}) == ntuple(identity, n) +end