In the documentation of StructArrays
, the Advanced techniques · StructArrays section describes how custom structs could be passed to a StructArray
constructor by overloading component
, createinstance
and staticschema
. However, I can’t figure how to apply this to a single case.
Here is a toy example
using StructArrays
# My custom struct
struct Mystruct
a::Int64
b::Int64
end
# Some data source returning NTuples
A = [(a=1,b=2), (a=3,b=4)]
# Desired container
struct ContainerS
A::StructVector{Mystruct}
end
# What works for now
struct ContainerA
A::AbstractVector{Mystruct}
end
Due to the identity in the named tuple and struct types and names, overloading staticschema
does not seem to be necessary. StructVector(A)
returns
julia> StructVector(A)
2-element StructArray(::Vector{Int64}, ::Vector{Int64}) with eltype @NamedTuple{a::Int64, b::Int64}:
(a = 1, b = 2)
(a = 3, b = 4)
obviously, Mystruct
is missing, but StructVector{Mystruct}(A,dims=1)
errrors with
ERROR: ArgumentError: only eltypes with fields are supported
so createinstance
and component
should be overloaded still.
In the meantime, if I do ContainerS(StructVector(A))
this errors with
ERROR: MethodError: Cannot
convert
an object of type
StructVector{@NamedTuple{a::Int64, b::Int64}, @NamedTuple{a::Vector{Int64}, b::Vector{Int64}}, Int64} to an object of type
StructVector{Mystruct}
but if I use a container with an abstract type ContainerA(StructVector(A))
it works, but I’m left with an abstract typed field.
Can somone explain
- How to properly overload
component
andcreateinstance
? - Why it works fine with the abstract typed field ?