Skip to content

Commit

Permalink
Fix some issues with new Dict constructors
Browse files Browse the repository at this point in the history
- Add apparently missing constructor for Dict(::((Any, Any)...))
- If either the keys or values has the same type for all Pairs, use
  that type
- Deprecations for removed constructors
  • Loading branch information
simonster committed Oct 8, 2014
1 parent 793cf3e commit a6715c6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,9 @@ const None = Union()
@deprecate median(v::AbstractArray; checknan::Bool=true) median(v)
@deprecate median(v::AbstractArray, region; checknan::Bool=true) median(v, region)
@deprecate median!(v::AbstractVector; checknan::Bool=true) median!(v)

@deprecate Dict{K,V}(ks::AbstractArray{K}, vs::AbstractArray{V}) Dict{K,V}(zip(ks, vs))
@deprecate Dict{K,V}(ks::(K...), vs::(V...)) Dict{K,V}(zip(ks, vs))
@deprecate Dict{K}(ks::(K...), vs::Tuple) Dict{K,Any}(zip(ks, vs))
@deprecate Dict{V}(ks::Tuple, vs::(V...)) Dict{Any,V}(zip(ks, vs))
@deprecate Dict(ks, vs) Dict{Any,Any}(zip(ks, vs))
19 changes: 12 additions & 7 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,23 @@ Dict(kv::()) = Dict()
const AnyDict = Dict{Any,Any}

# TODO: this can probably be simplified using `eltype` as a THT (Tim Holy trait)
Dict{K,V}(kv::((K,V)...,)) = Dict{K,V}(kv)
Dict{K }(kv::((K,Any)...,)) = Dict{K,Any}(kv)
Dict{V }(kv::((Any,V)...,)) = Dict{Any,V}(kv)
Dict{K,V}(kv::(Pair{K,V}...,)) = Dict{K,V}(kv)
Dict (kv::(Pair...,)) = Dict{Any,Any}(kv)
Dict{K,V}(kv::((K,V)...,)) = Dict{K,V}(kv)
Dict{K }(kv::((K,Any)...,)) = Dict{K,Any}(kv)
Dict{V }(kv::((Any,V)...,)) = Dict{Any,V}(kv)
Dict (kv::((Any,Any)...,)) = Dict{Any,Any}(kv)
Dict{K,V}(kv::(Pair{K,V}...,)) = Dict{K,V}(kv)
Dict{K} (kv::(Pair{K}...,)) = Dict{K,Any}(kv)
Dict{V} (kv::(Pair{TypeVar(:K),V}...,)) = Dict{Any,V}(kv)
Dict (kv::(Pair...,)) = Dict{Any,Any}(kv)

Dict{K,V}(kv::AbstractArray{(K,V)}) = Dict{K,V}(kv)
Dict{K,V}(kv::AbstractArray{Pair{K,V}}) = Dict{K,V}(kv)
Dict{K,V}(kv::Associative{K,V}) = Dict{K,V}(kv)

Dict{K,V}(ps::Pair{K,V}...) = Dict{K,V}(ps)
Dict (ps::Pair...) = Dict{Any,Any}(ps)
Dict{K,V}(ps::Pair{K,V}...) = Dict{K,V}(ps)
Dict{K} (ps::Pair{K}...,) = Dict{K,Any}(ps)
Dict{V} (ps::Pair{TypeVar(:K),V}...,) = Dict{Any,V}(ps)
Dict (ps::Pair...) = Dict{Any,Any}(ps)

Dict(kv) = dict_with_eltype(kv, eltype(kv))
dict_with_eltype{K,V}(kv, ::Type{(K,V)}) = Dict{K,V}(kv)
Expand Down
34 changes: 34 additions & 0 deletions test/collections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ end
_d = Dict("a"=>0)
@test isa([k for k in filter(x->length(x)==1, collect(keys(_d)))], Vector{Any})

let
d = Dict(((1, 2), (3, 4)))
@test d[1] === 2
@test d[3] === 4
d2 = Dict(1 => 2, 3 => 4)
d3 = Dict((1 => 2, 3 => 4))
@test d == d2 == d3
@test typeof(d) == typeof(d2) == typeof(d3) == Dict{Int,Int}

d = Dict(((1, 2), (3, "b")))
@test d[1] === 2
@test d[3] == "b"
d2 = Dict(1 => 2, 3 => "b")
d3 = Dict((1 => 2, 3 => "b"))
@test d == d2 == d3
@test typeof(d) == typeof(d2) == typeof(d3) == Dict{Int,Any}

d = Dict(((1, 2), ("a", 4)))
@test d[1] === 2
@test d["a"] === 4
d2 = Dict(1 => 2, "a" => 4)
d3 = Dict((1 => 2, "a" => 4))
@test d == d2 == d3
@test typeof(d) == typeof(d2) == typeof(d3) == Dict{Any,Int}

d = Dict(((1, 2), ("a", "b")))
@test d[1] === 2
@test d["a"] == "b"
d2 = Dict(1 => 2, "a" => "b")
d3 = Dict((1 => 2, "a" => "b"))
@test d == d2 == d3
@test typeof(d) == typeof(d2) == typeof(d3) == Dict{Any,Any}
end

# issue #1821
let
d = Dict{UTF8String, Vector{Int}}()
Expand Down

0 comments on commit a6715c6

Please sign in to comment.