Skip to content

Commit 81c3a76

Browse files
author
Andy Ferris
committed
Made less breaking, added news, empty works on more containers.
1 parent db20866 commit 81c3a76

File tree

8 files changed

+73
-8
lines changed

8 files changed

+73
-8
lines changed

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,13 @@ Deprecated or removed
12411241
12421242
* `EnvHash` has been renamed to `EnvDict` ([#24167]).
12431243
1244+
* Introduced the `empty` function, the functional pair to `empty!` which returns a new,
1245+
empty container ([#24390]).
1246+
1247+
* `similar(::Associative)` has been deprecated in favor of `empty(::Associative)`, and
1248+
`similar(::Associative, ::Pair{K, V})` has been deprecated in favour of
1249+
`empty(::Associative, K, V)` ([#24390]).
1250+
12441251
Command-line option changes
12451252
---------------------------
12461253

base/abstractarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,24 @@ indices of `A`.
566566
similar(f, shape::Tuple) = f(to_shape(shape)) # doesn't share well with Associative
567567
similar(f, dims::DimOrInd...) = similar(f, dims) # doesn't share well with Associative
568568

569+
"""
570+
empty(v::AbstractVector, [eltype])
571+
572+
Create an empty vector similar to `v`, optionally changing the `eltype`.
573+
574+
# Examples
575+
576+
```jldoctest
577+
julia> empty([1.0, 2.0, 3.0])
578+
0-element Array{Float64,1}
579+
580+
julia> empty([1.0, 2.0, 3.0], String)
581+
0-element Array{String,1}
582+
```
583+
"""
584+
empty(a::AbstractVector) = empty(a, eltype(a))
585+
empty(a::AbstractVector, ::Type{T}) where {T} = Vector{T}()
586+
569587
## from general iterable to any array
570588

571589
function copy!(dest::AbstractArray, src)

base/associative.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@ Custom `Associative` subtypes may choose which specific associative type is best
147147
return for the given value type and indices, by specializing on the three-argument
148148
signature. The default is to return a `Dict`.
149149
"""
150-
similar(a::Associative) = similar(a, valtype(a), keys(a))
150+
# v1.0: similar(a::Associative) = similar(a, valtype(a), keys(a))
151151
similar(a::Associative, ::Type{T}) where {T} = similar(a, T, keys(a))
152152
similar(a::Associative, inds) = similar(a, valtype(a), inds)
153153

154+
# disambiguation
155+
similar(a::Associative, t::Tuple) = similar(a, valtype(a), t)
156+
similar(a::Associative, d::DimOrInd) = similar(a, valtype(a), d)
157+
154158
"""
155159
empty(a::Associative, [index_type=keytype(a)], [value_type=valtype(a)])
156160

base/deprecated.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,7 @@ end
20742074
@deprecate EnvHash EnvDict
20752075

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

20792080
# END 0.7 deprecations

base/tuple.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,10 @@ any(x::Tuple{}) = false
328328
any(x::Tuple{Bool}) = x[1]
329329
any(x::Tuple{Bool, Bool}) = x[1]|x[2]
330330
any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3]
331+
332+
"""
333+
empty(x::Tuple)
334+
335+
Returns an empty tuple, `()`.
336+
"""
337+
empty(x::Tuple) = ()

test/abstractarray.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,5 +870,17 @@ end
870870
Base.convert(::Type{Array{T,n}}, a::Array{T,n}) where {T<:Number,n} = a
871871
Base.convert(::Type{Array{T,n}}, a::Array) where {T<:Number,n} =
872872
copy!(Array{T,n}(size(a)), a)
873-
@test isa(similar(Dict(:a=>1, :b=>2.0), Pair{Union{},Union{}}), Dict{Union{}, Union{}})
874-
end
873+
@test isa(empty(Dict(:a=>1, :b=>2.0), Union{}, Union{}), Dict{Union{}, Union{}})
874+
end
875+
876+
@testset "empty" begin
877+
@test isempty([])
878+
v = [1, 2, 3]
879+
v2 = empty(v)
880+
v3 = empty(v, Float64)
881+
@test !isempty(v)
882+
empty!(v)
883+
@test isempty(v)
884+
@test isempty(v2::Vector{Int})
885+
@test isempty(v3::Vector{Float64})
886+
end

test/dict.jl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ end
433433
@testset "similar" begin
434434
d = Dict(:a=>2, :b=>3)
435435

436-
d2 = similar(d)
437-
@test d2 isa typeof(d)
438-
@test length(keys(d2)) == 2
439-
@test :a keys(d2)
440-
@test :b keys(d2)
436+
# v1.0: d2 = similar(d)
437+
# v1.0: @test d2 isa typeof(d)
438+
# v1.0: @test length(keys(d2)) == 2
439+
# v1.0: @test :a ∈ keys(d2)
440+
# v1.0: @test :b ∈ keys(d2)
441441

442442
d3 = similar(d, Float64)
443443
@test d3 isa Dict{Symbol, Float64}
@@ -458,6 +458,20 @@ end
458458
@test 10 keys(d5)
459459
@test 20 keys(d5)
460460
@test 30 keys(d5)
461+
462+
d6 = similar(d, (10, 20, 30))
463+
@test d6 isa Dict{Int, Int}
464+
@test length(keys(d6)) == 3
465+
@test 10 keys(d6)
466+
@test 20 keys(d6)
467+
@test 30 keys(d6)
468+
469+
d7 = similar(d, Base.OneTo(3))
470+
@test d7 isa Dict{Int, Int}
471+
@test length(keys(d7)) == 3
472+
@test 1 keys(d7)
473+
@test 2 keys(d7)
474+
@test 3 keys(d7)
461475
end
462476

463477
@testset "generators, similar" begin

test/tuple.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ end
8383
@test Tuple{Int,Vararg{Any}}.ninitialized == 1
8484
@test Tuple{Any,Any,Vararg{Any}}.ninitialized == 2
8585
end
86+
87+
@test empty((1, 2.0, "c")) === ()
8688
end
8789

8890
@testset "size" begin

0 commit comments

Comments
 (0)