Skip to content

Commit

Permalink
Added dict-like constructors back in
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 committed Dec 22, 2017
1 parent 003d412 commit aca2eb4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
19 changes: 17 additions & 2 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,23 @@ end
IdDict() = IdDict{Any,Any}()
IdDict(kv::Tuple{}) = IdDict()

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

function IdDict(kv)
try
dict_with_eltype((K, V) -> IdDict{K, V}, kv, eltype(kv))
catch e
if !applicable(start, kv) || !all(x->isa(x,Union{Tuple,Pair}),kv)
throw(ArgumentError(
"IdDict(kv): kv needs to be an iterator of tuples or pairs"))
else
rethrow(e)
end
end
end

empty(d::IdDict, ::Type{K}, ::Type{V}) where {K, V} = IdDict{K,V}()

Expand Down
2 changes: 1 addition & 1 deletion base/codevalidation.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# Expr head => argument count bounds
const VALID_EXPR_HEADS = IdDict(
const VALID_EXPR_HEADS = IdDict{Any,Any}(
:call => 1:typemax(Int),
:invoke => 2:typemax(Int),
:static_parameter => 1:1,
Expand Down
7 changes: 1 addition & 6 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,6 @@ end
d = @inferred IdDict(Pair(1,1), Pair(2,2), Pair(3,3))
@test isa(d, IdDict)
@test d == IdDict(1=>1, 2=>2, 3=>3)
@test eltype(d) == Pair{Any,Any}

d = @inferred IdDict{Int,Int}(Pair(1,1), Pair(2,2), Pair(3,3))
@test d == IdDict{Int,Int}(1=>1, 2=>2, 3=>3)
@test d == IdDict{Any,Any}(1=>1, 2=>2, 3=>3)
@test eltype(d) == Pair{Int,Int}
@test_throws KeyError d[:a]
@test_throws ArgumentError d[:a] = 1
Expand All @@ -477,7 +472,7 @@ end
@test_throws ArgumentError IdDict{Float64,Int}(d)

# check that returned values are inferred
d = @inferred IdDict{Int,Int}(Pair(1,1), Pair(2,2), Pair(3,3))
d = @inferred IdDict(Pair(1,1), Pair(2,2), Pair(3,3))
@test 1 == @inferred d[1]
@inferred setindex!(d, -1, 10)
@test d[10] == -1
Expand Down

0 comments on commit aca2eb4

Please sign in to comment.