Skip to content

Commit

Permalink
Fix similar to only return SentinelArray when new type includes sentinel
Browse files Browse the repository at this point in the history
type
  • Loading branch information
quinnj committed Jun 28, 2020
1 parent b557b13 commit 4fbc943
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/SentinelArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4fbc943

Please sign in to comment.