Skip to content

Commit

Permalink
Merge pull request #17968 from JuliaLang/teh/similar_for
Browse files Browse the repository at this point in the history
Provide a better error for `similar` methods with Dict
  • Loading branch information
JeffBezanson authored Aug 23, 2016
2 parents 7199111 + eb95919 commit 2390055
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 2390055

Please sign in to comment.