20
20
"""
21
21
elconvert(T, A)
22
22
23
- Similar to `convert(T, A)`, but `T` refers to the eltype.
23
+ Similar to `convert(T, A)`, but `T` refers to the eltype. See also [`_to_eltype`](@ref).
24
24
25
25
# Examples
26
26
```jldoctest; setup = :(using EltypeExtensions: elconvert)
@@ -40,17 +40,26 @@ elconvert(::Type{T}, A::Tuple) where T = convert.(T, A)
40
40
"""
41
41
_to_eltype(T, S)
42
42
43
- Convert type `S` to have the `eltype` of `T`.
43
+ Convert type `S` to have the `eltype` of `T`. See also [`elconvert`](@ref).
44
44
"""
45
- _to_eltype (:: Type{T} , :: Type{S} ) where {T,S} = eltype (S) == S ? T : MethodError (_to_eltype, T, S)
45
+ _to_eltype (:: Type{T} , :: Type{S} ) where {T,S} = eltype (S) == S ? T : eltype (S) == T ? S : MethodError (_to_eltype, T, S)
46
46
_to_eltype (:: Type{T} , :: Type{Array{S,N}} ) where {T,S,N} = Array{T,N}
47
47
_to_eltype (:: Type{T} , :: Type{<:Set} ) where T = Set{T}
48
48
for TYP in (Adjoint, Diagonal, Hermitian, Symmetric, SymTridiagonal, Transpose)
49
+ @eval _to_eltype (:: Type{T} , :: Type{$TYP} ) where T = $ TYP{T}
50
+ @eval _to_eltype (:: Type{T} , :: Type{$TYP{S}} ) where {T,S} = $ TYP{T}
49
51
@eval _to_eltype (:: Type{T} , :: Type{$TYP{S,M}} ) where {T,S,M} = $ TYP{T,_to_eltype (T,M)}
50
52
@eval elconvert (:: Type{T} , A:: S ) where {T,S<: $TYP } = convert (_to_eltype (T, S), A)
51
53
end
52
54
_to_eltype (:: Type{T} , :: Type{<:UnitRange} ) where T<: Integer = UnitRange{T}
53
55
56
+ @static if VERSION >= v " 1.6"
57
+ _to_eltype (:: Type{CartesianIndex{N}} , :: Type{CartesianIndices{N,R}} ) where {N, R<: Tuple{Vararg{OrdinalRange{Int64, Int64}, N}} } = CartesianIndices{N,R}
58
+ else
59
+ _to_eltype (:: Type{CartesianIndex{N}} , :: Type{CartesianIndices{N,R}} ) where {N, R<: Tuple{Vararg{AbstractUnitRange{Int64},N}} } = CartesianIndices{N,R}
60
+ end
61
+ _to_eltype (:: Type{T} , :: Type{<:CartesianIndices} ) where T = Array{T}
62
+
54
63
@static if VERSION >= v " 1.7"
55
64
_to_eltype (:: Type{T} , :: Type{<:UnitRange} ) where T<: Real = StepRangeLen{T,Base. TwicePrecision{T},Base. TwicePrecision{T},Int}
56
65
else
0 commit comments