From 62c3894acfec5d86928a08c952fb1625a4a683a8 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 26 Aug 2022 17:19:26 +0300 Subject: [PATCH 1/2] use ConstructionBase.constructorof --- Project.toml | 4 +++- src/StructArrays.jl | 1 + src/interface.jl | 2 +- test/runtests.jl | 11 +++++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 9cf6fcd0..4edc34d7 100644 --- a/Project.toml +++ b/Project.toml @@ -4,15 +4,17 @@ version = "0.6.12" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" [compat] Adapt = "1, 2, 3" +ConstructionBase = "1" DataAPI = "1" -StaticArraysCore = "1.1" StaticArrays = "1.5.4" +StaticArraysCore = "1.1" Tables = "1" julia = "1.6" diff --git a/src/StructArrays.jl b/src/StructArrays.jl index 6fed453e..21c08844 100644 --- a/src/StructArrays.jl +++ b/src/StructArrays.jl @@ -1,6 +1,7 @@ module StructArrays using Base: tail +using ConstructionBase: constructorof export StructArray, StructVector, LazyRow, LazyRows export collect_structarray diff --git a/src/interface.jl b/src/interface.jl index 461e2d49..fe64721d 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -46,7 +46,7 @@ julia> StructArrays.createinstance(Complex{Float64}, (re=1.0, im=2.0)...) ``` """ function createinstance(::Type{T}, args...) where {T} - isconcretetype(T) ? bypass_constructor(T, args) : T(args...) + isconcretetype(T) ? bypass_constructor(T, args) : constructorof(T)(args...) end createinstance(::Type{T}, args...) where {T<:Tup} = T(args) \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index aabf8c90..ff145fdc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1253,3 +1253,14 @@ end @test zero(u) == StructArray([SVector(0.0)]) @test typeof(zero(u)) == typeof(StructArray([SVector(0.0)])) end + +@testset "parametric type" begin + struct PS{A, B} + a::A + b::B + end + + xs = StructArray([(a=1, b=2), (a=3, b=nothing)]) + ss = map(x -> PS(x.a, x.b), xs) + @test ss == [PS(1, 2), PS(3, nothing)] +end From 1bf728da69b7f31e551623feddae612fd7b7db7d Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 13 Jul 2023 09:49:27 +0300 Subject: [PATCH 2/2] Update src/interface.jl Co-authored-by: Mason Protter --- src/interface.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interface.jl b/src/interface.jl index fe64721d..f47d2b58 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -45,7 +45,7 @@ julia> StructArrays.createinstance(Complex{Float64}, (re=1.0, im=2.0)...) 1.0 + 2.0im ``` """ -function createinstance(::Type{T}, args...) where {T} +function createinstance(::Type{T}, args...)::T where {T} isconcretetype(T) ? bypass_constructor(T, args) : constructorof(T)(args...) end