Skip to content

Commit

Permalink
Provide a more useful error message for calling map on a Dict with a …
Browse files Browse the repository at this point in the history
…non-Pair return type
  • Loading branch information
timholy committed Aug 13, 2016
1 parent b066714 commit eb95919
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,6 @@ function similar(t::ImmutableDict)
end
return t
end

_similar_for{P<:Pair}(c::Dict, ::Type{P}, itr, isz) = similar(c, P)
_similar_for(c::Associative, T, itr, isz) = throw(ArgumentError("for Associatives, similar requires an element type of Pair;\n if calling map, consider a comprehension instead"))
5 changes: 5 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ 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)]

# generators, similar
d = Dict(:a=>"a")
@test @inferred(map(identity, d)) == d
@test @inferred(map(p->p.first=>p.second[1], d)) == Dict(:a=>'a')
@test_throws ArgumentError map(p->p.second, d)

# Issue 12451
@test_throws ArgumentError Dict(0)
Expand Down

0 comments on commit eb95919

Please sign in to comment.