Skip to content

Commit

Permalink
Made less breaking, added news, empty works on more containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ferris committed Oct 30, 2017
1 parent 996304c commit e1331dc
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 7 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,13 @@ Deprecated or removed

* `EnvHash` has been renamed to `EnvDict` ([#24167]).

* Introduced the `empty` function, the functional pair to `empty!` which returns a new,
empty container ([#24390]).

* `similar(::Associative)` has been deprecated in favor of `empty(::Associative)`, and
`similar(::Associative, ::Pair{K, V})` has been deprecated in favour of
`empty(::Associative, K, V)` ([#24390]).

Command-line option changes
---------------------------

Expand Down
18 changes: 18 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,24 @@ indices of `A`.
similar(f, shape::Tuple) = f(to_shape(shape)) # doesn't share well with Associative
similar(f, dims::DimOrInd...) = similar(f, dims) # doesn't share well with Associative

"""
empty(v::AbstractVector, [eltype])
Create an empty vector similar to `v`, optionally changing the `eltype`.
# Examples
```jldoctest
julia> empty([1.0, 2.0, 3.0])
0-element Array{Float64,1}
julia> empty([1.0, 2.0, 3.0], String)
0-element Array{String,1}
```
"""
empty(a::AbstractVector) = empty(a, eltype(a))
empty(a::AbstractVector, ::Type{T}) where {T} = Vector{T}()

## from general iterable to any array

function copy!(dest::AbstractArray, src)
Expand Down
6 changes: 5 additions & 1 deletion base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ Custom `Associative` subtypes may choose which specific associative type is best
return for the given value type and indices, by specializing on the three-argument
signature. The default is to return a `Dict`.
"""
similar(a::Associative) = similar(a, valtype(a), keys(a))
# v1.0: similar(a::Associative) = similar(a, valtype(a), keys(a))
similar(a::Associative, ::Type{T}) where {T} = similar(a, T, keys(a))
similar(a::Associative, inds) = similar(a, valtype(a), inds)

# disambiguation
similar(a::Associative, t::Tuple) = similar(a, eltype(a), t)
similar(a::Associative, d::DimOrInd) = similar(a, eltype(a), d)

"""
empty(a::Associative, [index_type=keytype(a)], [value_type=valtype(a)])
Expand Down
1 change: 1 addition & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,7 @@ end
@deprecate EnvHash EnvDict

# issue #24019
@deprecate similar(a::Associative) empty(a)
@deprecate similar(a::Associative, ::Type{Pair{K,V}}) where {K, V} empty(a, K, V)

# END 0.7 deprecations
Expand Down
7 changes: 7 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,10 @@ any(x::Tuple{}) = false
any(x::Tuple{Bool}) = x[1]
any(x::Tuple{Bool, Bool}) = x[1]|x[2]
any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3]

"""
empty(x::Tuple)
Returns an empty tuple, `()`.
"""
empty(x::Tuple) = ()
12 changes: 12 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -872,3 +872,15 @@ end
copy!(Array{T,n}(size(a)), a)
@test isa(similar(Dict(:a=>1, :b=>2.0), Pair{Union{},Union{}}), Dict{Union{}, Union{}})
end

@testet "empty" begin
@test isempty([])
v = [1, 2, ]
v2 = empty(v)
v3 = empty(v, Float64)
@test !isempty(v)
empty!(v)
@test isempty(v)
@test isempty(v2::Vector{Int})
@test isempty(v3::Vector{Float64})
end
12 changes: 6 additions & 6 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@test p[2] == 20
@test_throws BoundsError p[3]
@test_throws BoundsError p[false]
@test p[true] == 10
@test p[true] == 410
@test p[2.0] == 20
@test p[0x01] == 10
@test_throws InexactError p[2.3]
Expand Down Expand Up @@ -433,11 +433,11 @@ end
@testset "similar" begin
d = Dict(:a=>2, :b=>3)

d2 = similar(d)
@test d2 isa typeof(d)
@test length(keys(d2)) == 2
@test :a keys(d2)
@test :b keys(d2)
# v1.0: d2 = similar(d)
# v1.0: @test d2 isa typeof(d)
# v1.0: @test length(keys(d2)) == 2
# v1.0: @test :a ∈ keys(d2)
# v1.0: @test :b ∈ keys(d2)

d3 = similar(d, Float64)
@test d3 isa Dict{Symbol, Float64}
Expand Down
2 changes: 2 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ end
@test Tuple{Int,Vararg{Any}}.ninitialized == 1
@test Tuple{Any,Any,Vararg{Any}}.ninitialized == 2
end

@test empty((1, 2.0, "c")) === ()
end

@testset "size" begin
Expand Down

0 comments on commit e1331dc

Please sign in to comment.