Skip to content

Commit

Permalink
Merge pull request #11154 from garrison/vector-default-constructor
Browse files Browse the repository at this point in the history
RFC: Add Vector and Matrix constructors
  • Loading branch information
jakebolewski committed May 8, 2015
2 parents c912ee7 + d132337 commit f2e61a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ typealias StridedMatrix{T,A<:DenseArray,I<:Tuple{Vararg{RangeIndex}}} Union(Den
typealias StridedVecOrMat{T} Union(StridedVector{T}, StridedMatrix{T})

call{T}(::Type{Vector{T}}, m::Integer) = Array{T}(m)
call{T}(::Type{Vector{T}}) = Array{T}(0)
call(::Type{Vector}, m::Integer) = Array{Any}(m)
call(::Type{Vector}) = Array{Any}(0)

call{T}(::Type{Matrix{T}}, m::Integer, n::Integer) = Array{T}(m, n)
call{T}(::Type{Matrix{T}}) = Array{T}(0, 0)
call(::Type{Matrix}, m::Integer, n::Integer) = Array{Any}(m, n)
call(::Type{Matrix}) = Array{Any}(0, 0)

## Basic functions ##

Expand Down
19 changes: 19 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ ind = findin(a, b)
rt = Base.return_types(setindex!, Tuple{Array{Int32, 3}, UInt8, Vector{Int}, Float64, UnitRange{Int}})
@test length(rt) == 1 && rt[1] == Array{Int32, 3}

# construction
@test typeof(Vector{Int}(3)) == Vector{Int}
@test typeof(Vector{Int}()) == Vector{Int}
@test typeof(Vector(3)) == Vector{Any}
@test typeof(Vector()) == Vector{Any}
@test typeof(Matrix{Int}(2,3)) == Matrix{Int}
@test typeof(Matrix{Int}()) == Matrix{Int}
@test typeof(Matrix(2,3)) == Matrix{Any}
@test typeof(Matrix()) == Matrix{Any}

@test size(Vector{Int}(3)) == (3,)
@test size(Vector{Int}()) == (0,)
@test size(Vector(3)) == (3,)
@test size(Vector()) == (0,)
@test size(Matrix{Int}(2,3)) == (2,3)
@test size(Matrix{Int}()) == (0,0)
@test size(Matrix(2,3)) == (2,3)
@test size(Matrix()) == (0,0)

# get
let
A = reshape(1:24, 3, 8)
Expand Down

0 comments on commit f2e61a1

Please sign in to comment.