Skip to content

Commit

Permalink
Merge pull request #17 from putianyi889/version-0.0.4.5
Browse files Browse the repository at this point in the history
don't use code reflection
  • Loading branch information
putianyi889 authored Mar 16, 2024
2 parents 8a7933e + b8a3e1f commit 39ad44d
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/EltypeExtensions.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module EltypeExtensions

import Base: convert
import LinearAlgebra: AbstractQ # to support 1.0, not using package extensions
using LinearAlgebra # to support 1.0, not using package extensions
import LinearAlgebra: AbstractQ

export elconvert, basetype, baseconvert, precisiontype, precisionconvert

Expand All @@ -25,11 +26,10 @@ julia> typeof(elconvert(Float64, rand(Int, 3, 3)))
$(repr("text/plain", Matrix{Float64}))
```
"""
elconvert(::Type{T}, A::S) where {T,S} = eltype(S) == S ? convert(T, A) : throw(MethodError(elconvert, T, A))
elconvert(::Type{T}, A::S) where {T,S} = convert(_to_eltype(T, S), 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)
elconvert(::Type{T}, A::Tuple) where T = convert.(T, A)
if !(AbstractQ <: AbstractMatrix) # see https://github.com/JuliaLang/julia/pull/46196
elconvert(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A)
Expand All @@ -40,9 +40,11 @@ end
Convert type `S` to have the `eltype` of `T`.
"""
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : MethodError(_to_eltype, T, S)
_to_eltype(::Type{T}, ::Type{Array{S,N}}) where {T,S,N} = Array{T,N}
_to_eltype(::Type{T}, ::Type{Set}) where T = Set{T}
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = Base.return_types(elconvert, (Type{T}, S))
_to_eltype(::Type{T}, ::Type{<:Set}) where T = Set{T}
_to_eltype(::Type{T}, ::Type{Symmetric{S,M}}) where {T,S,M} = Symmetric{T,_to_eltype(T,M)}
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T = UnitRange{T}

nutype(x) = nutype(typeof(x))
nutype(T::Type) = throw(MethodError(nutype, T))
Expand Down

0 comments on commit 39ad44d

Please sign in to comment.