Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add undef #514

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ Currently, the `@compat` macro supports the following syntaxes:

* Constructor for `Matrix` from `UniformScaling` ([#24372], [#24657]).

* `Uninitialized` and `uninitialized` with corresponding `Array` constructors ([#24652]).
* `UndefInitializer` and `undef` with corresponding `Array` constructors ([#24652], [#26316]).

* `BitArray` constructors for `uninitialized` ([#24785]).
* `BitArray` constructors for `undef` ([#24785], [#26316]).

* `@compat finalizer(func, obj)` with the finalizer to run as the first argument and the object to be finalized
as the second ([#24605]).
Expand Down Expand Up @@ -589,3 +589,4 @@ includes this fix. Find the minimum version from there.
[#26089]: https://github.com/JuliaLang/julia/issues/26089
[#26149]: https://github.com/JuliaLang/julia/issues/26149
[#26156]: https://github.com/JuliaLang/julia/issues/26156
[#26316]: https://github.com/JuliaLang/julia/issues/26316
50 changes: 24 additions & 26 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -970,33 +970,31 @@ end
Base.IOContext(io::IOContext, arg1::Pair, arg2::Pair) = IOContext(IOContext(io, arg1), arg2)
end

# 0.7.0-DEV.2581
@static if !isdefined(Base, :Uninitialized)
if VERSION >= v"0.6.0"
include_string(@__MODULE__, """
struct Uninitialized end
Array{T}(::Uninitialized, args...) where {T} = Array{T}(args...)
Array{T,N}(::Uninitialized, args...) where {T,N} = Array{T,N}(args...)
Vector(::Uninitialized, args...) = Vector(args...)
Matrix(::Uninitialized, args...) = Matrix(args...)

BitArray{N}(::Uninitialized, args...) where {N} = BitArray{N}(args...)
BitArray(::Uninitialized, args...) = BitArray(args...)
""")
# 0.7.0-DEV.4527
@static if !isdefined(Base, :UndefInitializer)
import Base: Array, Matrix, Vector
@static if isdefined(Base, :Uninitialized)
useuninit(args) = (Base.uninitialized, args...)
else
include_string(@__MODULE__, """
immutable Uninitialized end
(::Type{Array{T}}){T}(::Uninitialized, args...) = Array{T}(args...)
(::Type{Array{T,N}}){T,N}(::Uninitialized, args...) = Array{T,N}(args...)
(::Type{Vector})(::Uninitialized, args...) = Vector(args...)
(::Type{Matrix})(::Uninitialized, args...) = Matrix(args...)

(::Type{BitArray{N}}){N}(::Uninitialized, args...) = BitArray{N}(args...)
(::Type{BitArray})(::Uninitialized, args...) = BitArray(args...)
""")
end
const uninitialized = Uninitialized()
export Uninitialized, uninitialized
useuninit(args) = args
end
struct UndefInitializer end
const undef = UndefInitializer()
export undef, UndefInitializer
Base.show(io::IO, ::UndefInitializer) =
print(io, "array initializer with undefined values")
Array{T}(::UndefInitializer, args...) where {T} = Array{T}(useuninit(args)...)
Array{T,N}(::UndefInitializer, args...) where {T,N} = Array{T,N}(useuninit(args)...)
Vector(::UndefInitializer, args...) = Vector(useuninit(args)...)
Matrix(::UndefInitializer, args...) = Matrix(useuninit(args)...)

BitArray{N}(::UndefInitializer, args...) where {N} = BitArray{N}(useuninit(args)...)
BitArray(::UndefInitializer, args...) = BitArray(useuninit(args)...)
end
@static if VERSION < v"0.7.0-DEV.2581"
export uninitialized, Uninitialized
const uninitialized = undef
const Uninitialized = UndefInitializer
end

# 0.7.0-DEV.1499
Expand Down
22 changes: 11 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -987,17 +987,17 @@ let a = [1 0 0; 0 1 0; 0 0 1]
@test Matrix(2.0I, 3, 3)::Matrix{Float64} == Matrix(2.0I, (3, 3))::Matrix{Float64} == 2a
end

# 0.7.0-DEV.2581
@test isa(Vector(uninitialized, 2), Vector{Any})
@test isa(Vector{Float64}(uninitialized, 2), Vector{Float64})
@test isa(Matrix(uninitialized, 2, 2), Matrix{Any})
@test isa(Matrix{Float64}(uninitialized, 2, 2), Matrix{Float64})
@test isa(Array{Float64}(uninitialized, 2, 2), Matrix{Float64})
@test isa(Array{Float64,3}(uninitialized, 2, 2, 2), Array{Float64,3})

# 0.7.0-DEV.2687
@test isa(BitVector(uninitialized, 2), BitVector)
@test isa(BitArray(uninitialized, 2, 2), BitMatrix)
# 0.7.0-DEV.2581, 0.7.0-DEV.4527
@test isa(Vector(undef, 2), Vector{Any})
@test isa(Vector{Float64}(undef, 2), Vector{Float64})
@test isa(Matrix(undef, 2, 2), Matrix{Any})
@test isa(Matrix{Float64}(undef, 2, 2), Matrix{Float64})
@test isa(Array{Float64}(undef, 2, 2), Matrix{Float64})
@test isa(Array{Float64,3}(undef, 2, 2, 2), Array{Float64,3})

# 0.7.0-DEV.2687, 0.7.0-DEV.4527
@test isa(BitVector(undef, 2), BitVector)
@test isa(BitArray(undef, 2, 2), BitMatrix)

# 0.7.0-DEV.1472
@test get(IOContext(IOBuffer(), :arg1=>true, :arg2=>true, :arg3=>true), :arg3, false)
Expand Down