Skip to content

Commit

Permalink
add undef (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Mar 12, 2018
1 parent 5ff9892 commit 1862df2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
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

0 comments on commit 1862df2

Please sign in to comment.