diff --git a/base/boot.jl b/base/boot.jl index ae4d02e553d09..5a60326b14c18 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -123,7 +123,7 @@ export SimpleVector, AbstractArray, DenseArray, NamedTuple, # special objects Function, CodeInfo, Method, MethodTable, TypeMapEntry, TypeMapLevel, - Module, Symbol, Task, Array, WeakRef, VecElement, + Module, Symbol, Task, Array, Uninitialized, uninitialized, WeakRef, VecElement, # numeric types Number, Real, Integer, Bool, Ref, Ptr, AbstractFloat, Float16, Float32, Float64, @@ -350,25 +350,45 @@ unsafe_convert(::Type{T}, x::T) where {T} = x const NTuple{N,T} = Tuple{Vararg{T,N}} -# primitive array constructors -Array{T,N}(d::NTuple{N,Int}) where {T,N} = - ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d) -Array{T,1}(d::NTuple{1,Int}) where {T} = Array{T,1}(getfield(d,1)) -Array{T,2}(d::NTuple{2,Int}) where {T} = Array{T,2}(getfield(d,1), getfield(d,2)) -Array{T,3}(d::NTuple{3,Int}) where {T} = Array{T,3}(getfield(d,1), getfield(d,2), getfield(d,3)) -Array{T,N}(d::Vararg{Int,N}) where {T,N} = ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d) -Array{T,1}(m::Int) where {T} = ccall(:jl_alloc_array_1d, Array{T,1}, (Any, Int), Array{T,1}, m) -Array{T,2}(m::Int, n::Int) where {T} = +## primitive Array constructors +struct Uninitialized end +const uninitialized = Uninitialized() +# type and dimensionality specified, accepting dims as series of Ints +Array{T,1}(::Uninitialized, m::Int) where {T} = + ccall(:jl_alloc_array_1d, Array{T,1}, (Any, Int), Array{T,1}, m) +Array{T,2}(::Uninitialized, m::Int, n::Int) where {T} = ccall(:jl_alloc_array_2d, Array{T,2}, (Any, Int, Int), Array{T,2}, m, n) -Array{T,3}(m::Int, n::Int, o::Int) where {T} = +Array{T,3}(::Uninitialized, m::Int, n::Int, o::Int) where {T} = ccall(:jl_alloc_array_3d, Array{T,3}, (Any, Int, Int, Int), Array{T,3}, m, n, o) +Array{T,N}(::Uninitialized, d::Vararg{Int,N}) where {T,N} = + ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d) +# type and dimensionality specified, accepting dims as tuples of Ints +Array{T,1}(::Uninitialized, d::NTuple{1,Int}) where {T} = Array{T,1}(uninitialized, getfield(d,1)) +Array{T,2}(::Uninitialized, d::NTuple{2,Int}) where {T} = Array{T,2}(uninitialized, getfield(d,1), getfield(d,2)) +Array{T,3}(::Uninitialized, d::NTuple{3,Int}) where {T} = Array{T,3}(uninitialized, getfield(d,1), getfield(d,2), getfield(d,3)) +Array{T,N}(::Uninitialized, d::NTuple{N,Int}) where {T,N} = ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d) +# type but not dimensionality specified +Array{T}(::Uninitialized, m::Int) where {T} = Array{T,1}(uninitialized, m) +Array{T}(::Uninitialized, m::Int, n::Int) where {T} = Array{T,2}(uninitialized, m, n) +Array{T}(::Uninitialized, m::Int, n::Int, o::Int) where {T} = Array{T,3}(uninitialized, m, n, o) +Array{T}(::Uninitialized, d::NTuple{N,Int}) where {T,N} = Array{T,N}(uninitialized, d) +# empty vector constructor +Array{T,1}() where {T} = Array{T,1}(uninitialized, 0) + +## preexisting Array constructors, i.e. without uninitialized, to deprecate +# type and dimensionality specified, accepting dims as series of Ints +Array{T,1}(m::Int) where {T} = Array{T,1}(uninitialized, m) +Array{T,2}(m::Int, n::Int) where {T} = Array{T,2}(uninitialized, m, n) +Array{T,3}(m::Int, n::Int, o::Int) where {T} = Array{T,3}(uninitialized, m, n, o) +Array{T,N}(d::Vararg{Int,N}) where {T,N} = Array{T,N}(uninitialized, d) +# type and dimensionality specified, accepting dims as tuples of Ints +Array{T,N}(d::NTuple{N,Int}) where {T,N} = Array{T,N}(uninitialized, d) +# type but not dimensionality specified +Array{T}(m::Int) where {T} = Array{T}(uninitialized, m) +Array{T}(m::Int, n::Int) where {T} = Array{T}(uninitialized, m, n) +Array{T}(m::Int, n::Int, o::Int) where {T} = Array{T}(uninitialized, m, n, o) +Array{T}(d::NTuple{N,Int}) where {T,N} = Array{T}(uninitialized, d) -Array{T}(d::NTuple{N,Int}) where {T,N} = Array{T,N}(d) -Array{T}(m::Int) where {T} = Array{T,1}(m) -Array{T}(m::Int, n::Int) where {T} = Array{T,2}(m, n) -Array{T}(m::Int, n::Int, o::Int) where {T} = Array{T,3}(m, n, o) - -Array{T,1}() where {T} = Array{T,1}(0) # primitive Symbol constructors function Symbol(s::String) diff --git a/base/docs/basedocs.jl b/base/docs/basedocs.jl index 81bbaa5aa70cb..a7d19dd2e480d 100644 --- a/base/docs/basedocs.jl +++ b/base/docs/basedocs.jl @@ -1297,6 +1297,42 @@ julia> B = Array{Float64}(2) # N determined by the input """ Array{T,N}(dims) +""" + Uninitialized + +Singleton type used in array initialization, indicating the array-constructor-caller +would like an uninitialized array. See also [`uninitialized`](@ref), +an alias for `Uninitialized()`. + +# Examples +```julia-repl +julia> Array{Float64,1}(Uninitialized(), 3) +3-element Array{Float64,1}: + 2.2752528595e-314 + 2.202942107e-314 + 2.275252907e-314 +``` +""" +Uninitialized + +""" + uninitialized + +Alias for `Uninitialized()`, which constructs an instance of the singleton type +[`Uninitialized`](@ref), used in array initialization to indicate the +array-constructor-caller would like an uninitialized array. + +# Examples +```julia-repl +julia> Array{Float64,1}(uninitialized, 3) +3-element Array{Float64,1}: + 2.2752528595e-314 + 2.202942107e-314 + 2.275252907e-314 +``` +""" +uninitialized + """ +(x, y...) diff --git a/base/sysimg.jl b/base/sysimg.jl index 0a3963d4872a2..724d7319475ce 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -130,17 +130,37 @@ include("abstractarray.jl") include("subarray.jl") include("reinterpretarray.jl") -# Array convenience converting constructors + +# ## dims-type-converting Array constructors for convenience +# type and dimensionality specified, accepting dims as series of Integers +Vector{T}(::Uninitialized, m::Integer) where {T} = Vector{T}(uninitialized, Int(m)) +Matrix{T}(::Uninitialized, m::Integer, n::Integer) where {T} = Matrix{T}(uninitialized, Int(m), Int(n)) +# type but not dimensionality specified, accepting dims as series of Integers +Array{T}(::Uninitialized, m::Integer) where {T} = Array{T,1}(uninitialized, Int(m)) +Array{T}(::Uninitialized, m::Integer, n::Integer) where {T} = Array{T,2}(uninitialized, Int(m), Int(n)) +Array{T}(::Uninitialized, m::Integer, n::Integer, o::Integer) where {T} = Array{T,3}(uninitialized, Int(m), Int(n), Int(o)) +Array{T}(::Uninitialized, d::Integer...) where {T} = Array{T}(uninitialized, convert(Tuple{Vararg{Int}}, d)) +# dimensionality but not type specified, accepting dims as series of Integers +Vector(::Uninitialized, m::Integer) = Vector{Any}(uninitialized, Int(m)) +Matrix(::Uninitialized, m::Integer, n::Integer) = Matrix{Any}(uninitialized, Int(m), Int(n)) +# empty vector constructor +Vector() = Vector{Any}(uninitialized, 0) + +## preexisting dims-type-converting Array constructors for convenience, i.e. without uninitialized, to deprecate +# type and dimensionality specified, accepting dims as series of Integers +Vector{T}(m::Integer) where {T} = Vector{T}(Int(m)) +Matrix{T}(m::Integer, n::Integer) where {T} = Matrix{T}(Int(m), Int(n)) +# type but not dimensionality specified, accepting dims as series of Integers Array{T}(m::Integer) where {T} = Array{T,1}(Int(m)) Array{T}(m::Integer, n::Integer) where {T} = Array{T,2}(Int(m), Int(n)) Array{T}(m::Integer, n::Integer, o::Integer) where {T} = Array{T,3}(Int(m), Int(n), Int(o)) Array{T}(d::Integer...) where {T} = Array{T}(convert(Tuple{Vararg{Int}}, d)) - -Vector() = Array{Any,1}(0) -Vector{T}(m::Integer) where {T} = Array{T,1}(Int(m)) -Vector(m::Integer) = Array{Any,1}(Int(m)) -Matrix{T}(m::Integer, n::Integer) where {T} = Matrix{T}(Int(m), Int(n)) +# dimensionality but not type specified, accepting dims as series of Integers +Vector(m::Integer) = Vector{Any}(Int(m)) Matrix(m::Integer, n::Integer) = Matrix{Any}(Int(m), Int(n)) +# empty vector constructor +Vector() = Vector{Any}(0) + include("associative.jl") diff --git a/doc/src/stdlib/arrays.md b/doc/src/stdlib/arrays.md index 99c2ceba831ea..383b886ccb895 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -8,6 +8,8 @@ Base.AbstractVector Base.AbstractMatrix Core.Array Core.Array(::Any) +Core.Uninitialized +Core.uninitialized Base.Vector Base.Vector(::Any) Base.Matrix