From a237986bf96fe5c851e62d45de3ad6398e876a22 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 20 Feb 2018 14:08:40 -0500 Subject: [PATCH] stop calling `supertype` in type trait functions (#26055) These cases should now use "triangular" dispatch instead. Also improve `eltype` of `AbstractDict` while we're at it. --- base/abstractarray.jl | 3 +-- base/abstractdict.jl | 20 +++++++++++++++----- base/array.jl | 1 - base/set.jl | 2 +- stdlib/LinearAlgebra/src/factorization.jl | 2 +- stdlib/Random/src/Random.jl | 2 +- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 1e520009dd8b5..ff81756a8cc2f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -138,8 +138,7 @@ julia> ndims(A) ``` """ ndims(::AbstractArray{T,N}) where {T,N} = N -ndims(::Type{AbstractArray{T,N}}) where {T,N} = N -ndims(::Type{T}) where {T<:AbstractArray} = ndims(supertype(T)) +ndims(::Type{<:AbstractArray{T,N}}) where {T,N} = N """ length(collection) -> Integer diff --git a/base/abstractdict.jl b/base/abstractdict.jl index a1fb7880c60e2..8e2b8a4269367 100644 --- a/base/abstractdict.jl +++ b/base/abstractdict.jl @@ -242,9 +242,8 @@ julia> keytype(Dict(Int32(1) => "foo")) Int32 ``` """ -keytype(::Type{AbstractDict{K,V}}) where {K,V} = K +keytype(::Type{<:AbstractDict{K,V}}) where {K,V} = K keytype(a::AbstractDict) = keytype(typeof(a)) -keytype(::Type{A}) where {A<:AbstractDict} = keytype(supertype(A)) """ valtype(type) @@ -257,8 +256,7 @@ julia> valtype(Dict(Int32(1) => "foo")) String ``` """ -valtype(::Type{AbstractDict{K,V}}) where {K,V} = V -valtype(::Type{A}) where {A<:AbstractDict} = valtype(supertype(A)) +valtype(::Type{<:AbstractDict{K,V}}) where {K,V} = V valtype(a::AbstractDict) = valtype(typeof(a)) """ @@ -447,7 +445,19 @@ function filter(f, d::AbstractDict) return df end -eltype(::Type{AbstractDict{K,V}}) where {K,V} = Pair{K,V} +function eltype(::Type{<:AbstractDict{K,V}}) where {K,V} + if @isdefined(K) + if @isdefined(V) + return Pair{K,V} + else + return Pair{K} + end + elseif @isdefined(V) + return Pair{k,V} where k + else + return Pair + end +end function isequal(l::AbstractDict, r::AbstractDict) l === r && return true diff --git a/base/array.jl b/base/array.jl index d847cbe19ac08..7435314caac50 100644 --- a/base/array.jl +++ b/base/array.jl @@ -87,7 +87,6 @@ UInt8 eltype(::Type) = Any eltype(::Type{Any}) = Any eltype(::Type{Bottom}) = throw(ArgumentError("Union{} does not have elements")) -eltype(t::DataType) = eltype(supertype(t)) eltype(x) = eltype(typeof(x)) import Core: arraysize, arrayset, arrayref diff --git a/base/set.jl b/base/set.jl index 4c43212e023da..5775af5f3fca4 100644 --- a/base/set.jl +++ b/base/set.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -eltype(::Type{AbstractSet{T}}) where {T} = T +eltype(::Type{<:AbstractSet{T}}) where {T} = @isdefined(T) ? T : Any struct Set{T} <: AbstractSet{T} dict::Dict{T,Nothing} diff --git a/stdlib/LinearAlgebra/src/factorization.jl b/stdlib/LinearAlgebra/src/factorization.jl index 45b808f378bb9..41353e56ca781 100644 --- a/stdlib/LinearAlgebra/src/factorization.jl +++ b/stdlib/LinearAlgebra/src/factorization.jl @@ -4,7 +4,7 @@ abstract type Factorization{T} end -eltype(::Type{Factorization{T}}) where {T} = T +eltype(::Type{<:Factorization{T}}) where {T} = T size(F::Adjoint{<:Any,<:Factorization}) = reverse(size(parent(F))) size(F::Transpose{<:Any,<:Factorization}) = reverse(size(parent(F))) diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index e7e3d2806e678..aa09b1e830d5f 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -95,7 +95,7 @@ const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}} abstract type Sampler{E} end -Base.eltype(::Type{Sampler{E}}) where {E} = E +Base.eltype(::Type{<:Sampler{E}}) where {E} = E # temporarily for BaseBenchmarks RangeGenerator(x) = Sampler(GLOBAL_RNG, x)