Skip to content

Commit

Permalink
add Array{T}(...), Vector{T}(...), and Matrix{T}(...) constructors
Browse files Browse the repository at this point in the history
closes #3214, closes #10075
  • Loading branch information
JeffBezanson committed Feb 12, 2015
1 parent 05dc3b0 commit 23a6995
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
13 changes: 10 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ New language features
and macros in packages and user code ([#8791]). Type `?@doc` at the repl
to see the current syntax and more information.

* New multidimensional iterators and index types for efficient
iteration over general AbstractArrays

Language changes
----------------

Expand All @@ -44,6 +41,8 @@ Language changes

* `Nothing` (the type of `nothing`) is renamed to `Void` ([#8423]).

* Arrays can be constructed with the syntax `Array{T}(m,n)` ([#3214], [#10075])

* `Dict` literal syntax `[a=>b,c=>d]` is replaced with `Dict(a=>b,c=>d)`.
`{a=>b}` is replaced with `Dict{Any,Any}(a=>b)`.
`(K=>V)[...]` is replaced with `Dict{K,V}(...)`.
Expand Down Expand Up @@ -79,6 +78,9 @@ Compiler improvements
Library improvements
--------------------

* New multidimensional iterators and index types for efficient
iteration over general AbstractArrays

* `LinAlg` improvements

* The `LinAlg` module is now exported.
Expand Down Expand Up @@ -980,6 +982,7 @@ Too numerous to mention.
[#3141]: https://github.com/JuliaLang/julia/issues/3141
[#3148]: https://github.com/JuliaLang/julia/issues/3148
[#3149]: https://github.com/JuliaLang/julia/issues/3149
[#3214]: https://github.com/JuliaLang/julia/issues/3214
[#3233]: https://github.com/JuliaLang/julia/issues/3233
[#3272]: https://github.com/JuliaLang/julia/issues/3272
[#3344]: https://github.com/JuliaLang/julia/issues/3344
Expand Down Expand Up @@ -1151,6 +1154,7 @@ Too numerous to mention.
[#8297]: https://github.com/JuliaLang/julia/issues/8297
[#8399]: https://github.com/JuliaLang/julia/issues/8399
[#8423]: https://github.com/JuliaLang/julia/issues/8423
[#8467]: https://github.com/JuliaLang/julia/issues/8467
[#8501]: https://github.com/JuliaLang/julia/issues/8501
[#8560]: https://github.com/JuliaLang/julia/issues/8560
[#8578]: https://github.com/JuliaLang/julia/issues/8578
Expand Down Expand Up @@ -1200,8 +1204,11 @@ Too numerous to mention.
[#9575]: https://github.com/JuliaLang/julia/issues/9575
[#9578]: https://github.com/JuliaLang/julia/issues/9578
[#9690]: https://github.com/JuliaLang/julia/issues/9690
[#9701]: https://github.com/JuliaLang/julia/issues/9701
[#9745]: https://github.com/JuliaLang/julia/issues/9745
[#9779]: https://github.com/JuliaLang/julia/issues/9779
[#9957]: https://github.com/JuliaLang/julia/issues/9957
[#10024]: https://github.com/JuliaLang/julia/issues/10024
[#10031]: https://github.com/JuliaLang/julia/issues/10031
[#10075]: https://github.com/JuliaLang/julia/issues/10075
[#10117]: https://github.com/JuliaLang/julia/issues/10117
3 changes: 3 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ typealias StridedVector{T,A<:DenseArray,I<:(RangeIndex...)} Union(DenseArray{T,
typealias StridedMatrix{T,A<:DenseArray,I<:(RangeIndex...)} Union(DenseArray{T,2}, SubArray{T,2,A,I})
typealias StridedVecOrMat{T} Union(StridedVector{T}, StridedMatrix{T})

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

## Basic functions ##

size(a::Array) = arraysize(a)
Expand Down
26 changes: 12 additions & 14 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,20 @@ macro goto(name::Symbol)
Expr(:symbolicgoto, name)
end

Array{T,N}(::Type{T}, d::NTuple{N,Int}) =
call{T,N}(::Type{Array{T}}, d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)
call{T}(::Type{Array{T}}, d::Integer...) = Array{T}(convert((Int...), d))

Array{T}(::Type{T}, m::Int) =
call{T}(::Type{Array{T}}, m::Integer) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Array{T}(::Type{T}, m::Int,n::Int) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m,n)
Array{T}(::Type{T}, m::Int,n::Int,o::Int) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m,n,o)

Array(T::Type, d::Int...) = Array(T, d)
Array(T::Type, d::Integer...) = Array(T, convert((Int...), d))

Array{T}(::Type{T}, m::Integer) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Array{T}(::Type{T}, m::Integer,n::Integer) =
call{T}(::Type{Array{T}}, m::Integer, n::Integer) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m, n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) =
call{T}(::Type{Array{T}}, m::Integer, n::Integer, o::Integer) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m, n, o)

# TODO: possibly turn these into deprecations
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d)
Array(T::Type, d::Integer...) = Array{T}(convert((Int...), d))
Array{T}(::Type{T}, m::Integer) = Array{T}(m)
Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T}(m,n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T}(m,n,o)
6 changes: 4 additions & 2 deletions doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ Basic functions
Constructors
------------

.. function:: Array(type, dims)
.. function:: Array(dims)

Construct an uninitialized dense array. ``dims`` may be a tuple or a series of integer arguments.
``Array{T}(dims)`` constructs an uninitialized dense array with element type ``T``.
``dims`` may be a tuple or a series of integer arguments.
The syntax ``Array(T, dims)`` is also available, but deprecated.

.. function:: getindex(type[, elements...])

Expand Down

1 comment on commit 23a6995

@kmsquire
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Please sign in to comment.