Skip to content

Commit

Permalink
fix fancy eltypes rebased (#280)
Browse files Browse the repository at this point in the history
* use ConstructionBase.constructorof

* Update src/interface.jl

Co-authored-by: Mason Protter <[email protected]>

---------

Co-authored-by: Alexander Plavin <[email protected]>
Co-authored-by: Mason Protter <[email protected]>
  • Loading branch information
3 people authored Aug 24, 2023
1 parent e2f0667 commit 0a5eb81
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ version = "0.6.15"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
Adapt = "1, 2, 3"
ConstructionBase = "1"
DataAPI = "1"
GPUArraysCore = "0.1.2"
StaticArrays = "1.5.6"
Expand Down
1 change: 1 addition & 0 deletions src/StructArrays.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module StructArrays

using Base: tail
using ConstructionBase: constructorof

export StructArray, StructVector, LazyRow, LazyRows
export collect_structarray
Expand Down
4 changes: 2 additions & 2 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ julia> StructArrays.createinstance(Complex{Float64}, (re=1.0, im=2.0)...)
1.0 + 2.0im
```
"""
function createinstance(::Type{T}, args...) where {T}
isconcretetype(T) ? bypass_constructor(T, args) : T(args...)
function createinstance(::Type{T}, args...)::T where {T}
isconcretetype(T) ? bypass_constructor(T, args) : constructorof(T)(args...)
end

createinstance(::Type{T}, args...) where {T<:Tup} = T(args)
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1466,3 +1466,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

0 comments on commit 0a5eb81

Please sign in to comment.