Skip to content

Commit da6cbe6

Browse files
committed
structarrays
1 parent d1e829b commit da6cbe6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
2929
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
3030
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
3131
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
32+
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
3233
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3334
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
3435

@@ -65,6 +66,7 @@ Static = "0.8, 1"
6566
StaticArrays = "1.5"
6667
Statistics = "1"
6768
StatsBase = "0.34"
69+
StructArrays = "0.7.0"
6870
Test = "1"
6971
Tricks = "0.1"
7072
julia = "1.10"

src/parameterized.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ function ConstructionBase.setproperties(
6767
return constructorof(P)(merge(params(d), nt))
6868
end
6969

70+
###############################################################################
71+
# StructArrays
72+
73+
# TODO: Can this be in an extension?
74+
75+
76+
import StructArrays
77+
78+
function StructArrays.staticschema(::Type{P}) where {N,P<:ParameterizedMeasure{N}}
79+
# Get the types from the NamedTuple parameter
80+
par_type = fieldtype(P, :par)
81+
# Use proper type construction for the component types
82+
types = Tuple{(fieldtype(par_type, n) for n in N)...}
83+
# Return NamedTuple type with parameter names
84+
return NamedTuple{N,types}
85+
end
86+
87+
function StructArrays.component(m::ParameterizedMeasure, key::Symbol)
88+
# Extract component from the parameters NamedTuple
89+
return getfield(getfield(m, :par), key)
90+
end
91+
92+
function StructArrays.createinstance(::Type{P}, args...) where {N,P<:ParameterizedMeasure{N}}
93+
# Create a new ParameterizedMeasure instance from components
94+
return constructorof(P)(NamedTuple{N}(args))
95+
end
96+
7097
###############################################################################
7198
# params
7299

0 commit comments

Comments
 (0)