Skip to content

Allow derived structs to override base fields with subtypes#681

Open
AdamGlustein wants to merge 1 commit intomainfrom
derived-struct-field-overrides-2
Open

Allow derived structs to override base fields with subtypes#681
AdamGlustein wants to merge 1 commit intomainfrom
derived-struct-field-overrides-2

Conversation

@AdamGlustein
Copy link
Collaborator

@AdamGlustein AdamGlustein commented Feb 23, 2026

Closes #517 , allows for derived struct types to specialize fields iff those fields are Python objects or csp.Structs.

For example,

class A(csp.Struct):
    field1: BaseObject
    field2: InnerBaseStruct

class B(A):
    field1: DerivedObject
    field2: InnerDerivedStruct

is allowed, but

class A(csp.Struct):
    field1: int

class B(A):
    field1: float

or

class A(csp.Struct):
    field1: DerivedObject

class B(A):
    field1: BaseObject

are not. Note that a lot of the changes in Struct.cpp is just a reordering of when we create the set/none masks; before we did this before adding base class fields, now we do it after.

@AdamGlustein AdamGlustein added the type: enhancement Issues and PRs related to improvements to existing features label Feb 23, 2026
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@AdamGlustein AdamGlustein force-pushed the derived-struct-field-overrides-2 branch from 9d51c23 to f718205 Compare February 24, 2026 14:35
//Setup masking bits for our fields
//NOTE we can be more efficient by sticking masks into any potential alignment gaps, dont want to spend time on it
//at this point
if( m_base )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this diff is hard to read. I think it was moved around to account for duplicate field names being allowed now, but can you explain what the actual diff was in this re-org

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, we would create the derived struct field masks for this level of struct before we process the base class fields
Now, since we may need to substitute a derived struct field into a base class field slot, we create the masks after

In the first section (adding in base fields), nothing changed until the loop on line 121 where before we would throw on a name collision, and now we check to see if its a valid override.
In the second (creating the masks), nothing changed except an index shift as we are iterating on the derived fields at the end of m_fields now, not the beginning

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

Labels

type: enhancement Issues and PRs related to improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow derived csp.Struct classes to override base class types if new types are more specialized

3 participants