Skip to content

Commit e1331dc

Browse files
author
Andy Ferris
committed
Made less breaking, added news, empty works on more containers.
1 parent 996304c commit e1331dc

File tree

8 files changed

+58
-7
lines changed

8 files changed

+58
-7
lines changed

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,13 @@ Deprecated or removed
12291229
12301230
* `EnvHash` has been renamed to `EnvDict` ([#24167]).
12311231
1232+
* Introduced the `empty` function, the functional pair to `empty!` which returns a new,
1233+
empty container ([#24390]).
1234+
1235+
* `similar(::Associative)` has been deprecated in favor of `empty(::Associative)`, and
1236+
`similar(::Associative, ::Pair{K, V})` has been deprecated in favour of
1237+
`empty(::Associative, K, V)` ([#24390]).
1238+
12321239
Command-line option changes
12331240
---------------------------
12341241

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, eltype(a), t)
156+
similar(a::Associative, d::DimOrInd) = similar(a, eltype(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
@@ -2073,6 +2073,7 @@ end
20732073
@deprecate EnvHash EnvDict
20742074

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

20782079
# 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,15 @@ end
872872
copy!(Array{T,n}(size(a)), a)
873873
@test isa(similar(Dict(:a=>1, :b=>2.0), Pair{Union{},Union{}}), Dict{Union{}, Union{}})
874874
end
875+
876+
@testet "empty" begin
877+
@test isempty([])
878+
v = [1, 2, ]
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@test p[2] == 20
2323
@test_throws BoundsError p[3]
2424
@test_throws BoundsError p[false]
25-
@test p[true] == 10
25+
@test p[true] == 410
2626
@test p[2.0] == 20
2727
@test p[0x01] == 10
2828
@test_throws InexactError p[2.3]
@@ -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}

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)