Skip to content

make similar mutable for non bits types #1196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ similar(::Type{A},s::Size{S}) where {A<:AbstractArray,S} = similar(A,eltype(A),s

similar(::A,::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = similar(A,T,s)

# defaults to built-in mutable types
similar(::Type{A},::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = mutable_similar_type(T,s,length_val(s))(undef)
# defaults to built-in mutable types for bits types
similar(::Type{A}, ::Type{T}, s::Size{S}) where {A<:AbstractArray,T,S} =
isbitstype(T) ?
mutable_similar_type(T, s, length_val(s))(undef) :
sizedarray_similar_type(T, s, length_val(s))(undef)

# both SizedArray and Array return SizedArray
similar(::Type{SA},::Type{T},s::Size{S}) where {SA<:SizedArray,T,S} = sizedarray_similar_type(T,s,length_val(s))(undef)
Expand Down
3 changes: 3 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ using StaticArrays, Test, LinearAlgebra
sv = @SVector [1,2,3]
sm = @SMatrix [1 2; 3 4]
sa = SArray{Tuple{1,1,1},Int,3,1}((1,))
sn = @SVector [1, missing]

@test isa(@inferred(similar(sv)), MVector{3,Int})
@test isa(@inferred(similar(sv, Float64)), MVector{3,Float64})
Expand All @@ -69,6 +70,8 @@ using StaticArrays, Test, LinearAlgebra
@test isa(@inferred(similar(sm, Float64)), MMatrix{2,2,Float64,4})
@test isa(@inferred(similar(sv, Size(3,3))), MMatrix{3,3,Int,9})
@test isa(@inferred(similar(sv, Float64, Size(3,3))), MMatrix{3,3,Float64,9})
@test isa(@inferred(similar(sn)), SizedVector{2, Union{Missing, Int}})
@test isa(@inferred(similar(sn, Float64, Size(3, 3))), MMatrix{3, 3, Float64, 9})

@test isa(@inferred(similar(sa)), MArray{Tuple{1,1,1},Int,3,1})
@test isa(@inferred(similar(SArray{Tuple{1,1,1},Int,3,1})), MArray{Tuple{1,1,1},Int,3,1})
Expand Down