From eb959193a50e5c28faff5cd1cd55b4cc86ca203b Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sat, 13 Aug 2016 06:19:17 -0500 Subject: [PATCH] Provide a more useful error message for calling map on a Dict with a non-Pair return type --- base/dict.jl | 3 +++ test/dict.jl | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/base/dict.jl b/base/dict.jl index d66b93377c7f8..69c41ece8d8cc 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -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")) diff --git a/test/dict.jl b/test/dict.jl index 45dcbbe998c8d..0af6b4d3807d7 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -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)