Skip to content

Commit aa45b8e

Browse files
committed
Add OneTo Array constructors
1 parent 2be031f commit aa45b8e

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

base/array.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ const DenseVector{T} = DenseArray{T,1}
6464
const DenseMatrix{T} = DenseArray{T,2}
6565
const DenseVecOrMat{T} = Union{DenseVector{T}, DenseMatrix{T}}
6666

67+
## More constructors
68+
Array{T,N}(inds::NTuple{N,<:OneTo}) where {T,N} = Array{T,N}(length.(inds))
69+
Array{T}(inds::NTuple{N,<:OneTo}) where {T,N} = Array{T,N}(inds)
70+
Array{T,N}(ind::OneTo, inds::Vararg{<:OneTo}) where {T,N} = Array{T,N}((ind, inds...))
71+
Array{T}(ind::OneTo, inds::Vararg{<:OneTo}) where T = Array{T}((ind, inds...))
72+
6773
## Basic functions ##
6874

6975
"""

base/boot.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ const NTuple{N,T} = Tuple{Vararg{T,N}}
355355
# primitive array constructors
356356
Array{T,N}(d::NTuple{N,Int}) where {T,N} =
357357
ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d)
358+
Array{T,0}(::Tuple{}) where {T} =
359+
ccall(:jl_new_array, Array{T,0}, (Any, Any), Array{T,0}, ())
358360
Array{T,1}(d::NTuple{1,Int}) where {T} = Array{T,1}(getfield(d,1))
359361
Array{T,2}(d::NTuple{2,Int}) where {T} = Array{T,2}(getfield(d,1), getfield(d,2))
360362
Array{T,3}(d::NTuple{3,Int}) where {T} = Array{T,3}(getfield(d,1), getfield(d,2), getfield(d,3))
@@ -366,6 +368,7 @@ Array{T,3}(m::Int, n::Int, o::Int) where {T} =
366368
ccall(:jl_alloc_array_3d, Array{T,3}, (Any, Int, Int, Int), Array{T,3}, m, n, o)
367369

368370
Array{T}(d::NTuple{N,Int}) where {T,N} = Array{T,N}(d)
371+
Array{T}(::Tuple{}) where {T} = Array{T,0}(())
369372
Array{T}(m::Int) where {T} = Array{T,1}(m)
370373
Array{T}(m::Int, n::Int) where {T} = Array{T,2}(m, n)
371374
Array{T}(m::Int, n::Int, o::Int) where {T} = Array{T,3}(m, n, o)

test/arrayops.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ end
327327
error("unexpected depwarn value")
328328
end
329329
@test_throws MethodError Array{Int,3}()
330+
331+
@test size(Array{Int}(Base.OneTo(3), Base.OneTo(2))) == (3,2)
332+
@test size(Array{Int}((Base.OneTo(3), Base.OneTo(2)))) == (3,2)
333+
@test size(Array{Int,2}(Base.OneTo(3), Base.OneTo(2))) == (3,2)
334+
@test size(Array{Int,2}((Base.OneTo(3), Base.OneTo(2)))) == (3,2)
335+
@test_throws MethodError Array{Int,3}((Base.OneTo(3), Base.OneTo(2)))
330336
end
331337
@testset "get" begin
332338
A = reshape(1:24, 3, 8)

0 commit comments

Comments
 (0)