Skip to content

Commit c4fcb08

Browse files
committed
Extend convert_eltype(T,X::Type) for more types X
1 parent 0ef434a commit c4fcb08

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/TypeUtils.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,8 @@ convert_eltype(::Type{T}, ::Type{X}) where {T,X} =
911911

912912
convert_eltype(::Type{T}, A::AbstractArray{T}) where {T} = A
913913
convert_eltype(::Type{T}, A::AbstractArray) where {T} = as(AbstractArray{T}, A)
914+
convert_eltype(::Type{T}, ::Type{<:Array{<:Any,N}}) where {T,N} = Array{T,N}
915+
convert_eltype(::Type{T}, ::Type{<:AbstractArray{<:Any,N}}) where {T,N} = AbstractArray{T,N}
914916

915917
# Convert element type for numbers.
916918
convert_eltype(::Type{T}, ::Type{<:Number}) where {T} = T
@@ -930,11 +932,13 @@ convert_eltype(::Type{T}, A::Union{OneTo,UnitRange}) where {T} =
930932
as(convert_eltype(T, typeof(A)), A)
931933

932934
# Convert element type for AbstractUnitRange{T} <: OrdinalRange{T,T}.
935+
convert_eltype(::Type{T}, ::Type{<:AbstractUnitRange}) where {T} = AbstractUnitRange{T}
933936
convert_eltype(::Type{T}, A::AbstractUnitRange{T}) where {T} = A
934937
convert_eltype(::Type{T}, A::AbstractUnitRange) where {T} =
935938
as(T, first(A)):as(T, last(A))
936939

937940
# Convert element type for other range types.
941+
convert_eltype(::Type{T}, ::Type{<:AbstractRange}) where {T} = AbstractRange{T}
938942
convert_eltype(::Type{T}, A::AbstractRange{T}) where {T} = A
939943
convert_eltype(::Type{T}, A::AbstractRange) where {T} =
940944
as(T, first(A)):as(T, step(A)):as(T, last(A))

test/runtests.jl

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,62 +338,73 @@ same_value_and_type(x::T, y::T) where {T} = (x === y) || (x == y)
338338
@testset "convert_eltype()" begin
339339
# Numbers.
340340
let A = rand(Float64), B = @inferred convert_eltype(Float32, A)
341-
@test convert_eltype(eltype(A), A) === A
341+
@test A === @inferred convert_eltype(eltype(A), A)
342342
@test B isa Float32
343343
@test B == Float32.(A)
344+
@test typeof(B) === @inferred convert_eltype(eltype(B), typeof(A))
344345
end
345346
# Abstract arrays.
346347
let A = rand(Float64, 2, 3), B = @inferred convert_eltype(Float32, A)
347-
@test convert_eltype(eltype(A), A) === A
348+
@test A === @inferred convert_eltype(eltype(A), A)
348349
@test B isa AbstractArray{Float32,ndims(A)}
349350
@test B == Float32.(A)
351+
@test typeof(B) === @inferred convert_eltype(eltype(B), typeof(A))
352+
@test AbstractArray{UInt8,2} === @inferred convert_eltype(UInt8, typeof(view(A,:,1:2:3)))
350353
end
351354
# Base.OneTo
352355
let A = OneTo{Int32}(7), B = @inferred convert_eltype(Int16, A)
353-
@test convert_eltype(eltype(A), A) === A
356+
@test A === @inferred convert_eltype(eltype(A), A)
354357
@test B isa OneTo{Int16}
355358
@test B == Int16.(A)
359+
@test typeof(B) === @inferred convert_eltype(eltype(B), typeof(A))
356360
end
357361
let A = OneTo(7), B = @inferred convert_eltype(Float32, A)
358-
@test convert_eltype(eltype(A), A) === A
362+
@test A === @inferred convert_eltype(eltype(A), A)
359363
@test B isa AbstractRange{Float32}
360364
@test B == Float32.(A)
365+
@test typeof(B) === @inferred convert_eltype(eltype(B), typeof(A))
361366
end
362367
# UnitRange
363368
let A = 2:8, B = @inferred convert_eltype(Float32, A)
364-
@test convert_eltype(eltype(A), A) === A
369+
@test A === @inferred convert_eltype(eltype(A), A)
365370
@test B isa AbstractRange{Float32}
366371
@test B == Float32.(A)
372+
@test typeof(B) === @inferred convert_eltype(eltype(B), typeof(A))
367373
end
368374
# OrdinalRange
369375
let A = 2.0:3.0:11.0, B = @inferred convert_eltype(Float32, A)
370-
@test convert_eltype(eltype(A), A) === A
376+
@test A === @inferred convert_eltype(eltype(A), A)
371377
@test B isa AbstractRange{Float32}
372378
@test B == Float32.(A)
379+
@test typeof(B) <: @inferred convert_eltype(eltype(B), typeof(A))
373380
end
374381
# LinRange
375382
let A = LinRange(-2.0, 3.0, 5), B = @inferred convert_eltype(Float32, A)
376-
@test convert_eltype(eltype(A), A) === A
383+
@test A === @inferred convert_eltype(eltype(A), A)
377384
@test B isa LinRange{Float32}
378385
@test B == Float32.(A)
386+
@test typeof(B) <: @inferred convert_eltype(eltype(B), typeof(A))
379387
end
380388
# Tuples.
381389
let A = (1, 2, 3) #= NTuple =#, B = @inferred convert_eltype(Float32, A)
382-
@test convert_eltype(eltype(A), A) === A
383-
@test B isa NTuple{<:Any,Float32}
390+
@test A === @inferred convert_eltype(eltype(A), A)
391+
@test B isa NTuple{3,Float32}
384392
@test B == Float32.(A)
393+
@test typeof(B) === @inferred convert_eltype(Float32, typeof(A))
385394
end
386-
let A = (1, 2.0, 3) #= not NTuple but Tuple =#, B = @inferred convert_eltype(Float32, A)
387-
@test convert_eltype(eltype(A), A) === A
388-
@test B isa NTuple{<:Any,Float32}
395+
let A = (1, 2.0, pi) #= not NTuple but Tuple =#, B = @inferred convert_eltype(Float32, A)
396+
@test A === @inferred convert_eltype(eltype(A), A)
397+
@test B isa NTuple{3,Float32}
389398
@test B == Float32.(A)
399+
@test typeof(B) === @inferred convert_eltype(Float32, typeof(A))
390400
end
391401
# Map `convert_eltype`.
392402
let A = rand(Float64, 2, 3), B = reshape(1:12, 3, 4)
393403
f = @inferred convert_eltype(Float32)
394404
@test isequal(f(A), convert_eltype(Float32, A))
395405
@test isequal(f(B), convert_eltype(Float32, B))
396406
end
407+
@test_throws ErrorException convert_eltype(Char, String)
397408
end
398409

399410
@testset "as_eltype()" begin

0 commit comments

Comments
 (0)