Skip to content

Request - multiple types & index field #264

Open

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

Activity

  1. jishnub commented on Feb 28, 2023

    @jishnub
    Member

    This isn't really how indexing works for arrays. Perhaps the solution would be to look for an array type that already allows indexing by keys, and wrap that in a StructArray?

  2. Lincoln-Hannah commented on Feb 28, 2023

    @Lincoln-Hannah
    Author

    Ok. what about the other suggestion. Allowing multiple struct types with some common fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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