diff --git a/base/dict.jl b/base/dict.jl index d66b93377c7f8..d0c5f1cec3c87 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -848,3 +848,10 @@ function similar(t::ImmutableDict) end return t end + +_similar_for{P<:Pair}(c::Dict, ::Type{P}, itr, isz::SizeUnknown) = similar(c, P) +_similar_for{P<:Pair}(c::Dict, ::Type{P}, itr, isz::HasLength) = similar(c, P) +_similar_for{P<:Pair}(c::Dict, ::Type{P}, itr, isz::HasShape) = similar(c, P) +_similar_for(c::Associative, T, itr, ::SizeUnknown) = Array{Any}(0) +_similar_for(c::Associative, T, itr, ::HasLength) = Array{Any}(length(itr)) +_similar_for(c::Associative, T, itr, ::HasShape) = Array{Any}(size(itr)) diff --git a/test/dict.jl b/test/dict.jl index 45dcbbe998c8d..893abff6726da 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 @inferred(map(p->p.second, d)) == Any["a"] # Issue 12451 @test_throws ArgumentError Dict(0)