Skip to content

Handling nested structs #85

@tk3369

Description

@tk3369

Hi Pietro. Great package!

Any thought about how to support nested structs? e.g.

julia> struct Point
           x::Int
           y::Int
       end

julia> struct Foo
           id::Int
           name::String
           pt::Point
       end

julia> x = [Foo(i, "foo$i", Point(2i, 3i)) for i in 1:3]
3-element Array{Foo,1}:
 Foo(1, "foo1", Point(2, 3))
 Foo(2, "foo2", Point(4, 6))
 Foo(3, "foo3", Point(6, 9))

I can do it like this:

julia> x2 = StructArray(
           id = [f.id for f in x],
           name = [f.name for f in x],
           pt = StructArray(f.pt for f in x))
3-element StructArray(::Array{Int64,1}, ::Array{String,1}, StructArray(::Array{Int64,1}, ::Array{Int64,1})) with eltype NamedTuple{(:id, :name, :pt),Tuple{Int64,String,Point}}:
 (id = 1, name = "foo1", pt = Point(2, 3))
 (id = 2, name = "foo2", pt = Point(4, 6))
 (id = 3, name = "foo3", pt = Point(6, 9))

It's a little bit unsatisfactory since I have to enumerate all non-struct fields (id and name in the above) manually...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions