Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use promote_typejoin() to choose type parameters in Dict constructor #25805

Merged
merged 2 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have one question - is the first method is needed only for performance reasons or covers some special case?

Copy link
Member Author

@nalimilan nalimilan Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's was there for performance, but that's not clear to me...

EDIT: FWIW, these constructors are very old, they have been introduced in 2d24d01 when Dict was called HashTable and before Pair existed.


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 @@ -139,6 +139,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 @@ -160,6 +160,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