# How to build typed StructArrays from NTuples

**URL:** <https://discourse.julialang.org/t/how-to-build-typed-structarrays-from-ntuples/112023>\
**Category:** General Usage\
**Tags:** question, constructors, structarrays\
**Created:** [March 23, 2024, 4:34pm UTC](https://discourse.julialang.org/t/how-to-build-typed-structarrays-from-ntuples/112023 "2024-03-23T16:34:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [March 23, 2024, 4:34pm UTC](https://discourse.julialang.org/t/how-to-build-typed-structarrays-from-ntuples/112023/1 "2024-03-23T16:34:52Z")

</div>

In the documentation of `StructArrays`, the [Advanced techniques · StructArrays](https://juliaarrays.github.io/StructArrays.jl/stable/advanced/) 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

```julia
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
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

1. How to properly overload `component` and `createinstance` ?
2. Why it works fine with the abstract typed field ?
