Skip to content

Request - multiple types & index field #264

@Lincoln-Hannah

Description

@Lincoln-Hannah

Would it be possible to have elements of multiple types with the same supertype, so that

  • fields common to all structs in the array can be called via Array.Field.
    - the array can be filtered for given type via Array(::Type) (or similar syntax)
  • If there is an "ID" field common to all structs, this can be used as a key.
abstract type MyType end

struct A<:MyType
    ID::String
    a::Int
end

struct B<:MyType
    ID::String
    b::Int
end

X = StructArray(      [ A("1",1), A("2",2), B("3",3), B("4",4) ],       parentType = MyType,      Key_Field = :ID   )


X.ID         # [1,2,3,4]

X["1"]       #  A("1",1)       select element by Key field (like a dictionary or named array)

X(::A)      # [A("1",1), A("2",2)]         filter for elements of type A

X(::A).a    # [1,2]                             reference field :a as it is common to elements of type A
X.a          #  Error                            not all elements contain this field

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