Skip to content

Commit

Permalink
Merge pull request #25805 from JuliaLang/nl/dict
Browse files Browse the repository at this point in the history
Use promote_typejoin() to choose type parameters in Dict constructor
  • Loading branch information
nalimilan authored Feb 14, 2018
2 parents 70eec0b + ea96757 commit 4c4aaaf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
6 changes: 2 additions & 4 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ copy(d::Dict) = Dict(d)

const AnyDict = Dict{Any,Any}

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

function Dict(kv)
try
Expand Down
8 changes: 4 additions & 4 deletions base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ Falls back to [`typejoin`](@ref).
promote_typejoin(@nospecialize(a), @nospecialize(b)) =
(@_pure_meta; join_types(a, b, promote_typejoin))
promote_typejoin(::Type{Nothing}, ::Type{T}) where {T} =
isconcretetype(T) ? Union{T, Nothing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Nothing} : Any
promote_typejoin(::Type{T}, ::Type{Nothing}) where {T} =
isconcretetype(T) ? Union{T, Nothing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Nothing} : Any
promote_typejoin(::Type{Missing}, ::Type{T}) where {T} =
isconcretetype(T) ? Union{T, Missing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Missing} : Any
promote_typejoin(::Type{T}, ::Type{Missing}) where {T} =
isconcretetype(T) ? Union{T, Missing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Missing} : Any
promote_typejoin(::Type{Nothing}, ::Type{Missing}) = Union{Nothing, Missing}
promote_typejoin(::Type{Missing}, ::Type{Nothing}) = Union{Nothing, Missing}
promote_typejoin(::Type{Nothing}, ::Type{Nothing}) = Nothing
Expand Down
2 changes: 2 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ for T in (Nothing, Missing)
@test Base.promote_typejoin(Int, String) === Any
@test Base.promote_typejoin(Int, Union{Float64, T}) === Any
@test Base.promote_typejoin(Int, Union{String, T}) === Any
@test Base.promote_typejoin(T, Union{}) === T
@test Base.promote_typejoin(Union{}, T) === T
end

@test promote_type(Bool,Bottom) === Bool
Expand Down
13 changes: 13 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ end
@test d == Dict{Real,Real}(2.0=>3.0, 1=>2)
end

@testset "type of Dict constructed from varargs of Pairs" begin
@test Dict(1=>1, 2=>2.0) isa Dict{Int,Real}
@test Dict(1=>1, 2.0=>2) isa Dict{Real,Int}
@test Dict(1=>1.0, 2.0=>2) isa Dict{Real,Real}

for T in (Nothing, Missing)
@test Dict(1=>1, 2=>T()) isa Dict{Int,Union{Int,T}}
@test Dict(1=>T(), 2=>2) isa Dict{Int,Union{Int,T}}
@test Dict(1=>1, T()=>2) isa Dict{Union{Int,T},Int}
@test Dict(T()=>1, 2=>2) isa Dict{Union{Int,T},Int}
end
end

@test_throws KeyError Dict("a"=>2)[Base.secret_table_token]

@testset "issue #1821" begin
Expand Down

0 comments on commit 4c4aaaf

Please sign in to comment.