Skip to content
Open
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.5.6"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
julia = "1.6"
ConstructionBase = "1"
StaticArraysCore = "~1.3.0"

[extras]
Expand Down
3 changes: 3 additions & 0 deletions src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Base: getindex, setindex!, size, similar, vec, show, length, convert, pro

import Statistics: mean

import ConstructionBase: constructorof

using Random
import Random: rand, randn, randexp, rand!, randn!, randexp!
using Core.Compiler: return_type
Expand Down Expand Up @@ -128,6 +130,7 @@ include("svd.jl")
include("qr.jl")
include("deque.jl")
include("flatten.jl")
include("constructorof.jl")
include("io.jl")
include("pinv.jl")

Expand Down
9 changes: 9 additions & 0 deletions src/constructorof.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# keep the size when reconstructing arrays
# eltype can be different
constructorof(sa::Type{<:SArray{S}}) where {S} = SArray{S}
constructorof(sa::Type{<:MArray{S}}) where {S} = MArray{S}

# don't keep neither size nor eltype for vectors:
# both are unambiguously determined by the values
constructorof(::Type{<:SVector}) = SVector
constructorof(::Type{<:MVector}) = MVector
25 changes: 25 additions & 0 deletions test/constructorof.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using StaticArrays
using Test
using ConstructionBase: constructorof

@testset "constructorof" begin
sa = @SVector [2, 4, 6, 8]
sa2 = constructorof(typeof(sa))((3.0, 5.0, 7.0, 9.0))
@test sa2 === @SVector [3.0, 5.0, 7.0, 9.0]

ma = @MMatrix [2.0 4.0; 6.0 8.0]
ma2 = constructorof(typeof(ma))((1, 2, 3, 4))
@test ma2 isa MArray{Tuple{2,2},Int,2,4}
@test all(ma2 .=== @MMatrix [1 3; 2 4])

for T in (SVector, MVector)
@test constructorof(T)((1, 2, 3))::T == T((1, 2, 3))
@test constructorof(T{3})((1, 2, 3))::T == T((1, 2, 3))
@test constructorof(T{3})((1, 2))::T == T((1, 2))
@test constructorof(T{3, Symbol})((1, 2, 3))::T == T((1, 2, 3))
@test constructorof(T{3, Symbol})((1, 2))::T == T((1, 2))
@test constructorof(T{3, X} where {X})((1, 2, 3))::T == T((1, 2, 3))
@test constructorof(T{3, X} where {X})((1, 2))::T == T((1, 2))
@test constructorof(T{X, Symbol} where {X})((1, 2, 3))::T == T((1, 2, 3))
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ if TEST_GROUP ∈ ["", "all", "group-B"]
addtests("chol.jl") # hermitian_type(::Type{Any}) for block algorithm
addtests("deque.jl")
addtests("flatten.jl")
addtests("constructorof.jl")
addtests("io.jl")
addtests("svd.jl")
end