Skip to content

Commit 0a5eb81

Browse files
pieveraplavinMasonProtter
authored
fix fancy eltypes rebased (#280)
* use ConstructionBase.constructorof * Update src/interface.jl Co-authored-by: Mason Protter <mason.protter@icloud.com> --------- Co-authored-by: Alexander Plavin <alexander@plav.in> Co-authored-by: Mason Protter <mason.protter@icloud.com>
1 parent e2f0667 commit 0a5eb81

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ version = "0.6.15"
44

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

1213
[compat]
1314
Adapt = "1, 2, 3"
15+
ConstructionBase = "1"
1416
DataAPI = "1"
1517
GPUArraysCore = "0.1.2"
1618
StaticArrays = "1.5.6"

src/StructArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module StructArrays
22

33
using Base: tail
4+
using ConstructionBase: constructorof
45

56
export StructArray, StructVector, LazyRow, LazyRows
67
export collect_structarray

src/interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ julia> StructArrays.createinstance(Complex{Float64}, (re=1.0, im=2.0)...)
4545
1.0 + 2.0im
4646
```
4747
"""
48-
function createinstance(::Type{T}, args...) where {T}
49-
isconcretetype(T) ? bypass_constructor(T, args) : T(args...)
48+
function createinstance(::Type{T}, args...)::T where {T}
49+
isconcretetype(T) ? bypass_constructor(T, args) : constructorof(T)(args...)
5050
end
5151

5252
createinstance(::Type{T}, args...) where {T<:Tup} = T(args)

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,3 +1466,14 @@ end
14661466
@test zero(u) == StructArray([SVector(0.0)])
14671467
@test typeof(zero(u)) == typeof(StructArray([SVector(0.0)]))
14681468
end
1469+
1470+
@testset "parametric type" begin
1471+
struct PS{A, B}
1472+
a::A
1473+
b::B
1474+
end
1475+
1476+
xs = StructArray([(a=1, b=2), (a=3, b=nothing)])
1477+
ss = map(x -> PS(x.a, x.b), xs)
1478+
@test ss == [PS(1, 2), PS(3, nothing)]
1479+
end

0 commit comments

Comments
 (0)