diff --git a/src/SentinelArrays.jl b/src/SentinelArrays.jl index 7598b7d..51e6fe2 100644 --- a/src/SentinelArrays.jl +++ b/src/SentinelArrays.jl @@ -114,7 +114,11 @@ Base.convert(::Type{SentinelArray}, arr::AbstractArray{T}) where {T} = convert(S Base.convert(::Type{SentinelVector{T}}, arr::AbstractArray) where {T} = convert(SentinelArray{T}, arr) function Base.similar(A::SentinelArray{T, N, S, V}, ::Type{T2}, dims::Dims{N2}) where {T, N, S, V, T2, N2} - SentinelArray{Core.Compiler.typesubtract(T2, V)}(undef, dims) + if T2 >: V + SentinelArray{Core.Compiler.typesubtract(T2, V)}(undef, dims) + else + similar(parent(A), T2, dims) + end end Base.empty(A::SentinelVector{T}, ::Type{U}=T) where {T, U} = SentinelVector{U}(undef, 0) diff --git a/test/runtests.jl b/test/runtests.jl index 400a5c0..d507310 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,14 +35,14 @@ y = similar(x, 20) y = similar(x, (10, 10)) @test eltype(parent(x)) == eltype(parent(y)) @test y[1] === missing -y = similar(x, Float64) +y = similar(x, Union{Missing, Float64}) @test eltype(parent(y)) === Float64 @test y[1] === missing -y = similar(x, Float64, 10) +y = similar(x, Union{Missing, Float64}, 10) @test eltype(parent(y)) === Float64 @test y[1] === missing @test length(y) == 10 -y = similar(x, Float64, (10, 10)) +y = similar(x, Union{Missing, Float64}, (10, 10)) @test eltype(parent(y)) === Float64 @test y[1] === missing