Skip to content

Commit

Permalink
fix #12451 by providing a better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Donner committed Aug 4, 2015
1 parent d34ce59 commit ca58ab2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,19 @@ 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))
function Dict(kv)
try
Base.dict_with_eltype(kv, eltype(kv))
catch e
if any(x->isempty(methods(x, (typeof(kv),))), [start, next, done]) ||
!all(x->isa(x,Union{Tuple,Pair}),kv)
throw(ArgumentError("Dict(kv): kv needs to be an iterator of tuples or pairs"))
else
rethrow(e)
end
end
end

dict_with_eltype{K,V}(kv, ::Type{Tuple{K,V}}) = Dict{K,V}(kv)
dict_with_eltype{K,V}(kv, ::Type{Pair{K,V}}) = Dict{K,V}(kv)
dict_with_eltype(kv, t) = Dict{Any,Any}(kv)
Expand Down
6 changes: 6 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,9 @@ end
d = Dict('a'=>1, 'b'=>1, 'c'=> 3)
@test [d[k] for k in keys(d)] == [d[k] for k in eachindex(d)] ==
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]


# Issue 12451
@test_throws ArgumentError Dict(0)
@test_throws ArgumentError Dict([1])
@test_throws ArgumentError Dict([(1,2),0])

0 comments on commit ca58ab2

Please sign in to comment.