From 83f532d08ddd25f82443341a8defd9edf1dd4d23 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <912396513@qq.com> Date: Sat, 16 Mar 2024 14:23:16 +0000 Subject: [PATCH] use convert instead of constructor --- src/EltypeExtensions.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/EltypeExtensions.jl b/src/EltypeExtensions.jl index d7af4ad..84c9a8b 100644 --- a/src/EltypeExtensions.jl +++ b/src/EltypeExtensions.jl @@ -25,10 +25,11 @@ julia> typeof(elconvert(Float64, rand(Int, 3, 3))) $(repr("text/plain", Matrix{Float64})) ``` """ -elconvert(::Type{T}, A::AbstractArray) where T = AbstractArray{T}(A) -elconvert(::Type{T}, A::AbstractRange) where T = T(first(A)):T(step(A)):T(last(A)) -elconvert(::Type{T}, A::AbstractUnitRange) where T<:Integer = AbstractUnitRange{T}(A) -elconvert(::Type{T}, A::AbstractSet) where T = AbstractSet{T}(A) +elconvert(::Type{T}, A::S) where {T,S} = eltype(S) == S ? convert(T, A) : throw(MethodError(elconvert, T, A)) +elconvert(::Type{T}, A::AbstractArray) where T = convert(AbstractArray{T}, A) +elconvert(::Type{T}, A::AbstractRange) where T = map(T, A) +elconvert(::Type{T}, A::AbstractUnitRange) where T<:Integer = convert(AbstractUnitRange{T}, A) +elconvert(::Type{T}, A::Set) where T = convert(Set{T}, A) if !(AbstractQ <: AbstractMatrix) # see https://github.com/JuliaLang/julia/pull/46196 elconvert(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A) end