Skip to content

Commit

Permalink
Deprecate or fix old/broken DefaultDict constructors
Browse files Browse the repository at this point in the history
* Dict and OrderedDict constructors have changed, and
  DefaultDict/DefaultOrderedDict haven't kept up.
* The changes:

   Dict(KeyType, ValType) => Dict{KeyType, ValType}()
   Dict(ks, vs) is deprectated

* This deprecates working versions of these from DefaultDict
  and friends, and fixes broken calls which attempts to
  use these old constructors.
  • Loading branch information
kmsquire committed Nov 19, 2016
1 parent 7e38347 commit 40da2d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/DataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ module DataStructures
export status
export deref_key, deref_value, deref, advance, regress

# Deprecations

# Remove when Julia 0.6 is released
@deprecate iter(s::Stack) s
@deprecate iter(q::Queue) q

# Remove when Julia 0.7 (or whatever version is after v0.6) is released
@deprecate DefaultDictBase(default, ks::AbstractArray, vs::AbstractArray) DefaultDictBase(default, zip(ks, vs))
@deprecate DefaultDictBase(default, ks, vs) DefaultDictBase(default, zip(ks, vs))
@deprecate DefaultDictBase{K,V}(::Type{K}, ::Type{V}, default) DefaultDictBase{K,V}(default)

@deprecate DefaultDict(default, ks, vs) DefaultDict(default, zip(ks, vs))
@deprecate DefaultDict{K,V}(::Type{K}, ::Type{V}, default) DefaultDict{K,V}(default)

@deprecate DefaultOrderedDict(default, ks, vs) DefaultOrderedDict(default, zip(ks, vs))
@deprecate DefaultOrderedDict{K,V}(::Type{K}, ::Type{V}, default) DefaultOrderedDict{K,V}(default)
end
22 changes: 8 additions & 14 deletions src/default_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,22 @@ immutable DefaultDictBase{K,V,F,D} <: Associative{K,V}

DefaultDictBase(x::F, d::DefaultDictBase) = (check_D(D,K,V); DefaultDictBase(x, d.d))
DefaultDictBase(x::F, d::D=D()) = (check_D(D,K,V); new(x, d))
DefaultDictBase(x, ks, vs) = (check_D(D,K,V); new(x, D(ks,vs)))
end

# Constructors

DefaultDictBase() = throw(ArgumentError("no default specified"))
DefaultDictBase(k,v) = throw(ArgumentError("no default specified"))

# TODO: these mimic similar Dict constructors, but may not be needed
DefaultDictBase{K,V,F}(default::F, ks::AbstractArray{K}, vs::AbstractArray{V}) =
DefaultDictBase{K,V,F,Dict{K,V}}(default,ks,vs)
DefaultDictBase{F}(default::F,ks,vs) = DefaultDictBase{Any,Any,F,Dict}(default, ks, vs)

# syntax entry points
DefaultDictBase{F}(default::F) = DefaultDictBase{Any,Any,F,Dict}(default)
DefaultDictBase{K,V,F}(::Type{K}, ::Type{V}, default::F) = DefaultDictBase{K,V,F,Dict}(default)
DefaultDictBase{K,V,F}(default::F, kv::AbstractArray{Tuple{K,V}}) = DefaultDictBase{K,V,F,Dict}(default, kv)
DefaultDictBase{K,V,F}(default::F, ps::Pair{K,V}...) = DefaultDictBase{K,V,F,Dict}(default, ps...)
DefaultDictBase{F,D<:Associative}(default::F, d::D) = ((K,V)=eltype(d); DefaultDictBase{K,V,F,D}(default, d))

# Constructor for DefaultDictBase{Int,Float64}(0.0)
(::Type{DefaultDictBase{K,V}}){K,V,F}(default::F) = DefaultDictBase{K,V,F,Dict{K,V}}(default)

# Functions

# most functions are simply delegated to the wrapped dictionary
Expand Down Expand Up @@ -78,26 +74,24 @@ for dict in [:Dict, :OrderedDict]
$DefaultDict(x, d::$DefaultDict) = $DefaultDict(x, d.d)
$DefaultDict(x, d::$dict) = new(DefaultDictBase{K,V,F,$dict{K,V}}(x, d))
$DefaultDict(x) = new(DefaultDictBase{K,V,F,$dict{K,V}}(x))
$DefaultDict(x, ks, vs) = new(DefaultDictBase{K,V,F,$dict{K,V}}(x,ks,vs))
end

## Constructors

$DefaultDict() = throw(ArgumentError("$DefaultDict: no default specified"))
$DefaultDict(k,v) = throw(ArgumentError("$DefaultDict: no default specified"))

# TODO: these mimic similar Dict constructors, but may not be needed
$DefaultDict{K,V,F}(default::F, ks::AbstractArray{K}, vs::AbstractArray{V}) = $DefaultDict{K,V,F}(default,ks,vs)
$DefaultDict{F}(default::F,ks,vs) = $DefaultDict{Any,Any,F}(default, ks, vs)

# syntax entry points
$DefaultDict{F}(default::F) = $DefaultDict{Any,Any,F}(default)
$DefaultDict{K,V,F}(::Type{K}, ::Type{V}, default::F) = $DefaultDict{K,V,F}(default)
$DefaultDict{K,V,F}(default::F, kv::AbstractArray{Tuple{K,V}}) = $DefaultDict{K,V,F}(default, kv)
$DefaultDict{K,V,F}(default::F, ps::Pair{K,V}...) = $DefaultDict{K,V,F}(default, ps...)

$DefaultDict{F}(default::F, d::Associative) = ((K,V)= (Base.keytype(d), Base.valtype(d)); $DefaultDict{K,V,F}(default, $dict(d)))

# Constructor syntax: DefaultDictBase{Int,Float64}(default)
@compat (::Type{$DefaultDict{K,V}}){K,V}() = throw(ArgumentError("$DefaultDict: no default specified"))
@compat (::Type{$DefaultDict{K,V}}){K,V,F}(default::F) = $DefaultDict{K,V,F}(default)

## Functions

# Most functions are simply delegated to the wrapped DefaultDictBase object
Expand All @@ -112,7 +106,7 @@ for dict in [:Dict, :OrderedDict]
end


## If/when a SortedDict becomes available, this should be uncommented to provide a DefaultSortedDict
## This should be uncommented to provide a DefaultSortedDict

# immutable DefaultSortedDict{K,V,F} <: Associative{K,V}
# d::DefaultDictBase{K,V,F,SortedDict{K,V}}
Expand Down
7 changes: 4 additions & 3 deletions test/test_default_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
# construction
@test_throws ArgumentError DefaultDict()
@test_throws ArgumentError DefaultDict(AbstractString, Int)
@test_throws ArgumentError DefaultDict{AbstractString, Int}()

@test isa(DefaultDict(0.0, 1 => 1.0), DefaultDict{Int,Float64,Float64})

# empty dictionary
d = DefaultDict(Char, Int, 1)
d = DefaultDict{Char, Int}(1)
@test length(d) == 0
@test isempty(d)
@test d['c'] == 1
Expand Down Expand Up @@ -67,10 +68,10 @@ s = similar(d)

# construction
@test_throws ArgumentError DefaultOrderedDict()
@test_throws ArgumentError DefaultOrderedDict(AbstractString, Int)
@test_throws ArgumentError DefaultOrderedDict{AbstractString, Int}()

# empty dictionary
d = DefaultOrderedDict(Char, Int, 1)
d = DefaultOrderedDict{Char, Int}(1)
@test length(d) == 0
@test isempty(d)
@test d['c'] == 1
Expand Down

0 comments on commit 40da2d5

Please sign in to comment.