Skip to content

Commit

Permalink
make similar mutable for non bits types (#1196)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
adienes and mateuszbaran authored Sep 23, 2023
1 parent bba1f48 commit 217e6f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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

2 comments on commit 217e6f1

@mateuszbaran
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92066

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.4 -m "<description of version>" 217e6f10a01433b8a60b679622065bc8702da923
git push origin v1.6.4

Please sign in to comment.