Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request - multiple types & index field #264

Open
Lincoln-Hannah opened this issue Feb 22, 2023 · 2 comments
Open

Request - multiple types & index field #264

Lincoln-Hannah opened this issue Feb 22, 2023 · 2 comments

Comments

@Lincoln-Hannah
Copy link

Lincoln-Hannah commented Feb 22, 2023

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
@jishnub
Copy link
Member

jishnub commented Feb 28, 2023

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?

@Lincoln-Hannah
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants