From e2aebba4c4bb047b2d1eb781d33a0fc357623f52 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Wed, 12 Dec 2018 10:50:03 +0100 Subject: [PATCH] Force specialization on the type argument of `_similar_for` (#30331) (cherry picked from commit 891e2abdd778ae053451863f0ee81950834f881c) --- base/array.jl | 10 ++++++---- base/dict.jl | 3 ++- base/set.jl | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/base/array.jl b/base/array.jl index a10678d44b1c6..dcea65bcadba6 100644 --- a/base/array.jl +++ b/base/array.jl @@ -528,10 +528,12 @@ function _collect(::Type{T}, itr, isz::SizeUnknown) where T end # make a collection similar to `c` and appropriate for collecting `itr` -_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0) -_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer)) -_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, axes(itr)) -_similar_for(c, T, itr, isz) = similar(c, T) +_similar_for(c::AbstractArray, ::Type{T}, itr, ::SizeUnknown) where {T} = similar(c, T, 0) +_similar_for(c::AbstractArray, ::Type{T}, itr, ::HasLength) where {T} = + similar(c, T, Int(length(itr)::Integer)) +_similar_for(c::AbstractArray, ::Type{T}, itr, ::HasShape) where {T} = + similar(c, T, axes(itr)) +_similar_for(c, ::Type{T}, itr, isz) where {T} = similar(c, T) """ collect(collection) diff --git a/base/dict.jl b/base/dict.jl index 1c1d0f51b9cd7..cf25738d95cd5 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -760,4 +760,5 @@ isempty(t::ImmutableDict) = !isdefined(t, :parent) empty(::ImmutableDict, ::Type{K}, ::Type{V}) where {K, V} = ImmutableDict{K,V}() _similar_for(c::Dict, ::Type{Pair{K,V}}, itr, isz) where {K, V} = empty(c, K, V) -_similar_for(c::AbstractDict, T, itr, isz) = throw(ArgumentError("for AbstractDicts, similar requires an element type of Pair;\n if calling map, consider a comprehension instead")) +_similar_for(c::AbstractDict, ::Type{T}, itr, isz) where {T} = + throw(ArgumentError("for AbstractDicts, similar requires an element type of Pair;\n if calling map, consider a comprehension instead")) diff --git a/base/set.jl b/base/set.jl index ab64837aa9ce1..583d5e350536e 100644 --- a/base/set.jl +++ b/base/set.jl @@ -34,7 +34,7 @@ empty(s::AbstractSet{T}, ::Type{U}=T) where {T,U} = Set{U}() # by default, a Set is returned emptymutable(s::AbstractSet{T}, ::Type{U}=T) where {T,U} = Set{U}() -_similar_for(c::AbstractSet, T, itr, isz) = empty(c, T) +_similar_for(c::AbstractSet, ::Type{T}, itr, isz) where {T} = empty(c, T) function show(io::IO, s::Set) print(io, "Set(")