Skip to content

undef initializer with custom array type? #332

@MilesCranmer

Description

@MilesCranmer

What is the right way to initialize an empty StructArray with a custom array type without multiple allocations?

I tried this but no luck:

julia> StructArray{ComplexF64, 1, @NamedTuple{re::Vector{Float64},im::Vector{Float64}}}(undef, 5)
ERROR: MethodError: no method matching (StructVector{ComplexF64, @NamedTuple{re::Vector{Float64}, im::Vector{Float64}}})(::UndefInitializer, ::Int64)
The type `StructVector{ComplexF64, @NamedTuple{re::Vector{Float64}, im::Vector{Float64}}}` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  (StructArray{T, N, C})(::Any) where {T, N, C<:Union{Tuple, NamedTuple}}
   @ StructArrays ~/.julia/packages/StructArrays/n5wxA/src/structarray.jl:16

The current example in the docs implies you would have to initialize each array manually:

julia> using StructArrays, StaticArrays

julia> x = StructArray([SVector{2}(1,2) for i = 1:5])
5-element StructArray(::Vector{Tuple{Int64, Int64}}) with eltype SVector{2, Int64}:
 [1, 2]
 [1, 2]
 [1, 2]
 [1, 2]
 [1, 2]

but ideally I wouldn't need to make these extra allocations.

I'm interested in using this in particular in combination with FixedSizeArrays.jl. @giordano @nsajko any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions