1
1
module EltypeExtensions
2
2
3
3
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
5
6
6
7
export elconvert, basetype, baseconvert, precisiontype, precisionconvert
7
8
@@ -25,11 +26,10 @@ julia> typeof(elconvert(Float64, rand(Int, 3, 3)))
25
26
$(repr (" text/plain" , Matrix{Float64}))
26
27
```
27
28
"""
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 )
29
30
elconvert (:: Type{T} , A:: AbstractArray ) where T = convert (AbstractArray{T}, A)
30
31
elconvert (:: Type{T} , A:: AbstractRange ) where T = map (T, A)
31
32
elconvert (:: Type{T} , A:: AbstractUnitRange ) where T<: Integer = convert (AbstractUnitRange{T}, A)
32
- elconvert (:: Type{T} , A:: Set ) where T = convert (Set{T}, A)
33
33
elconvert (:: Type{T} , A:: Tuple ) where T = convert .(T, A)
34
34
if ! (AbstractQ <: AbstractMatrix ) # see https://github.com/JuliaLang/julia/pull/46196
35
35
elconvert (:: Type{T} , A:: AbstractQ ) where T = convert (AbstractQ{T}, A)
40
40
41
41
Convert type `S` to have the `eltype` of `T`.
42
42
"""
43
+ _to_eltype (:: Type{T} , :: Type{S} ) where {T,S} = eltype (S) == S ? T : MethodError (_to_eltype, T, S)
43
44
_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}
46
48
47
49
nutype (x) = nutype (typeof (x))
48
50
nutype (T:: Type ) = throw (MethodError (nutype, T))
0 commit comments