Skip to content

Commit 217e6f1

Browse files
make similar mutable for non bits types (#1196)
* make similar mutable for non bits types * add quick test * Update src/abstractarray.jl Co-authored-by: Mateusz Baran <[email protected]> --------- Co-authored-by: Mateusz Baran <[email protected]>
1 parent bba1f48 commit 217e6f1

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/abstractarray.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ similar(::Type{A},s::Size{S}) where {A<:AbstractArray,S} = similar(A,eltype(A),s
128128

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

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

134137
# both SizedArray and Array return SizedArray
135138
similar(::Type{SA},::Type{T},s::Size{S}) where {SA<:SizedArray,T,S} = sizedarray_similar_type(T,s,length_val(s))(undef)

test/abstractarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ using StaticArrays, Test, LinearAlgebra
5959
sv = @SVector [1,2,3]
6060
sm = @SMatrix [1 2; 3 4]
6161
sa = SArray{Tuple{1,1,1},Int,3,1}((1,))
62+
sn = @SVector [1, missing]
6263

6364
@test isa(@inferred(similar(sv)), MVector{3,Int})
6465
@test isa(@inferred(similar(sv, Float64)), MVector{3,Float64})
@@ -69,6 +70,8 @@ using StaticArrays, Test, LinearAlgebra
6970
@test isa(@inferred(similar(sm, Float64)), MMatrix{2,2,Float64,4})
7071
@test isa(@inferred(similar(sv, Size(3,3))), MMatrix{3,3,Int,9})
7172
@test isa(@inferred(similar(sv, Float64, Size(3,3))), MMatrix{3,3,Float64,9})
73+
@test isa(@inferred(similar(sn)), SizedVector{2, Union{Missing, Int}})
74+
@test isa(@inferred(similar(sn, Float64, Size(3, 3))), MMatrix{3, 3, Float64, 9})
7275

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

0 commit comments

Comments
 (0)