You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function StructArrays.createinstance(::Type{T}, x, args...) where {T<:MyType}
209
-
names, types =getnamestypes(T)
210
-
MyType(x, NamedTuple{names, types}(args))
206
+
function StructArrays.createinstance(::Type{MyType{T, NT}}, x, args...) where {T, NT}
207
+
MyType(x, NT(args))
211
208
end
212
209
213
210
s = [MyType(rand(), a=1, b=2) for i in1:10]
214
211
StructArray(s)
215
212
```
216
213
214
+
In the above example, our `MyType` was composed of `data` of type `Float64` and `rest` of type `NamedTuple`. In many practical cases where there are custom types involved it's hard for StructArrays to automatically widen the types in case they are heterogeneous. The following example demonstrates a widening method in that scenario.
functionmeta_table(data, rest::NT) where NT<:NamedTuple
242
+
F = MyType{eltype(data), StructArrays.eltypes(NT)}
243
+
returnStructArray{F}(; data=data, rest...)
244
+
end
245
+
246
+
meta_table(s)
247
+
```
248
+
249
+
The above strategy has been tested and implemented in [GeometryBasics.jl](https://github.com/JuliaGeometry/GeometryBasics.jl).
250
+
217
251
## Advanced: mutate-or-widen style accumulation
218
252
219
253
StructArrays provides a function `StructArrays.append!!(dest, src)` (unexported) for "mutate-or-widen" style accumulation. This function can be used via [`BangBang.append!!`](https://juliafolds.github.io/BangBang.jl/dev/#BangBang.append!!) and [`BangBang.push!!`](https://juliafolds.github.io/BangBang.jl/dev/#BangBang.push!!) as well.
@@ -305,69 +339,3 @@ julia> s
305
339
Foo(44, "d")
306
340
Foo(55, "e")
307
341
```
308
-
309
-
In the above example "for structures with non-standard data layout" our `MyType` was composed of `data` of type `Float64` and `rest` of type `NamedTuple`. In many practical cases where there are custom types involved it's hard for StructArrays to automatically widen the types in case they are heterogeneous. The following example demonstrates a widening method in that scenario.
310
-
311
-
```julia
312
-
struct MyType1{T, Names, Types}
313
-
data::T
314
-
rest::NamedTuple{Names, Types}
315
-
end
316
-
317
-
MyType1(x; kwargs...) =MyType1(x, values(kwargs))
318
-
319
-
# and a source of custom type data
320
-
struct location{U}
321
-
x::U
322
-
y::U
323
-
end
324
-
struct region{G<:Array{location}}
325
-
area::G
326
-
end
327
-
328
-
function Base.getproperty(x::MyType1, field::Symbol)
0 commit comments