Skip to content
Closed
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 @@ -6,9 +6,11 @@ version = "1.1.1"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"

[compat]
julia = "1"
ConstructionBase = "1.0, 1.1.0"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
2 changes: 2 additions & 0 deletions src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Base: getindex, setindex!, size, similar, vec, show, length, convert, pro

import Statistics: mean

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

if Base.VERSION >= v"1.4.2"
include("precompile.jl")
Expand Down
15 changes: 15 additions & 0 deletions src/constructorof.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct SArrayConstructor{S,N,L} end
struct MArrayConstructor{S,N,L} end
struct SizedArrayConstructor{S,N,M} end

(::SArrayConstructor{S,N,L})(data::NTuple{L,T}) where {S,T,N,L} = SArray{S,T,N,L}(data)
(::MArrayConstructor{S,N,L})(data::NTuple{L,T}) where {S,T,N,L} = MArray{S,T,N,L}(data)
(::SizedArrayConstructor{S,N,M})(data::AbstractArray{T,M}) where {S,T,N,M} =
SizedArray{S,T,N,M}(data)

ConstructionBase.constructorof(sa::Type{<:SArray{S,<:Any,N,L}}) where {S,N,L} =
SArrayConstructor{S,N,L}()
ConstructionBase.constructorof(sa::Type{<:MArray{S,<:Any,N,L}}) where {S,N,L} =
MArrayConstructor{S,N,L}()
ConstructionBase.constructorof(sa::Type{<:SizedArray{S,<:Any,N,M}}) where {S,N,M} =
SizedArrayConstructor{S,N,M}()
17 changes: 17 additions & 0 deletions test/constructorof.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using StaticArrays, Test, ConstructionBase

@testset "constructorof" begin
sa = @SVector [2, 4, 6, 8]
sa2 = ConstructionBase.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 = ConstructionBase.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])

sz = SizedArray{Tuple{2,2}}([1 2;3 4])
sz2 = ConstructionBase.constructorof(typeof(sz))([:a :b; :c :d])
@test sz2 == SizedArray{Tuple{2,2}}([:a :b; :c :d])
@test typeof(sz2) <: SizedArray{Tuple{2,2},Symbol,2,2}
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ addtests("SUnitRange.jl")
addtests("SizedArray.jl")
addtests("SDiagonal.jl")
addtests("SHermitianCompact.jl")
addtests("constructorof.jl")

addtests("ambiguities.jl")
addtests("custom_types.jl")
Expand Down