Add stateless type support for ad-hoc discriminated unions #17
Merged
Add stateless type support for ad-hoc discriminated unions #17
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds support for marking type members of ad-hoc discriminated unions as "stateless" to optimize memory usage. Stateless types store only the discriminator index without allocating backing fields, with member accessors returning default(T).
Changes:
- Added
T1IsStatelessthroughT5IsStatelessproperties to both generic and non-generic union attributes - Modified source generator to skip backing field allocation for stateless members and return
default(T)from accessors - Added comprehensive test coverage (15+ new snapshot tests) for equality, pattern matching, hash codes, and conversions
Reviewed changes
Copilot reviewed 56 out of 56 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| AdHocUnionAttribute.cs | Added TXIsStateless properties with auto-nullable reference type handling |
| AdHocUnionCodeGenerator.cs | Modified code generation to handle stateless members in Switch/Map/Equals/GetHashCode/ToString |
| AdHocUnionSettings.cs | Updated to read stateless settings from attributes |
| AdHocUnionMemberTypeSetting.cs | Added IsStateless property to member type settings |
| AttributeDataExtensions.cs | Added FindTxIsStateless helper method |
| Test files (Switch.cs, Map.cs, etc.) | Added comprehensive tests for stateless type behavior |
| Sample files | Added demo showing practical API response scenarios with stateless types |
| Documentation | Updated CLAUDE.md and CLAUDE-ATTRIBUTES.md with stateless type information |
tests results 29 files ± 0 29 suites ±0 38m 19s ⏱️ +23s Results for commit 9d23026. ± Comparison against base commit 3400675. This pull request removes 1 and adds 89 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
e3002b1 to
3b29a97
Compare
3b29a97 to
9d23026
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the ability to mark type members of ad-hoc discriminated unions as "stateless" for memory optimization. Stateless types carry no instance data—only the discriminator
index is stored in the union, while member accessors return
default(T).What's New
API Changes
T1IsStateless,T2IsStateless,T3IsStateless,T4IsStateless,T5IsStatelessproperties to both:UnionAttribute<T1, T2, ...>AdHocUnionAttribute(...)IsTproperties work normally (discriminator-based)AsTaccessors returndefault(T)TXIsNullableReferenceType = trueExample Usage