Skip to content

Commit 39ad44d

Browse files
authored
Merge pull request #17 from putianyi889/version-0.0.4.5
don't use code reflection
2 parents 8a7933e + b8a3e1f commit 39ad44d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/EltypeExtensions.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module EltypeExtensions
22

33
import Base: convert
4-
import LinearAlgebra: AbstractQ # to support 1.0, not using package extensions
4+
using LinearAlgebra # to support 1.0, not using package extensions
5+
import LinearAlgebra: AbstractQ
56

67
export elconvert, basetype, baseconvert, precisiontype, precisionconvert
78

@@ -25,11 +26,10 @@ julia> typeof(elconvert(Float64, rand(Int, 3, 3)))
2526
$(repr("text/plain", Matrix{Float64}))
2627
```
2728
"""
28-
elconvert(::Type{T}, A::S) where {T,S} = eltype(S) == S ? convert(T, A) : throw(MethodError(elconvert, T, A))
29+
elconvert(::Type{T}, A::S) where {T,S} = convert(_to_eltype(T, S), A)
2930
elconvert(::Type{T}, A::AbstractArray) where T = convert(AbstractArray{T}, A)
3031
elconvert(::Type{T}, A::AbstractRange) where T = map(T, A)
3132
elconvert(::Type{T}, A::AbstractUnitRange) where T<:Integer = convert(AbstractUnitRange{T}, A)
32-
elconvert(::Type{T}, A::Set) where T = convert(Set{T}, A)
3333
elconvert(::Type{T}, A::Tuple) where T = convert.(T, A)
3434
if !(AbstractQ <: AbstractMatrix) # see https://github.com/JuliaLang/julia/pull/46196
3535
elconvert(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A)
@@ -40,9 +40,11 @@ end
4040
4141
Convert type `S` to have the `eltype` of `T`.
4242
"""
43+
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : MethodError(_to_eltype, T, S)
4344
_to_eltype(::Type{T}, ::Type{Array{S,N}}) where {T,S,N} = Array{T,N}
44-
_to_eltype(::Type{T}, ::Type{Set}) where T = Set{T}
45-
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = Base.return_types(elconvert, (Type{T}, S))
45+
_to_eltype(::Type{T}, ::Type{<:Set}) where T = Set{T}
46+
_to_eltype(::Type{T}, ::Type{Symmetric{S,M}}) where {T,S,M} = Symmetric{T,_to_eltype(T,M)}
47+
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T = UnitRange{T}
4648

4749
nutype(x) = nutype(typeof(x))
4850
nutype(T::Type) = throw(MethodError(nutype, T))

0 commit comments

Comments
 (0)