Fix @SwiftSnapshot macro to support generic types #30
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.
Problem
The
@SwiftSnapshotmacro failed to compile when applied to generic types, producing the error:This occurred because Swift prohibits static stored properties in generic types, but the macro was generating:
Solution
The fix uses computed properties for all types (both generic and non-generic) to simplify the codebase. Since computed properties work for both cases, this eliminates the need for conditional logic.
For all types (unified approach):
Implementation
static var) for all types instead of differentiating between generic and non-genericisGenericType()helper functiongeneratePropertiesArray()to generate only computed propertiesTesting
genericStruct()to verify correct code generationmacroWithGenericType()to verify compilation and runtime behaviorBenefits
✅ Simpler codebase - Removed ~30 lines of conditional logic
✅ Unified approach - Same property style for all types
✅ Fully compatible - Works for both generic and non-generic types
✅ No breaking changes - Computed properties behave identically to stored properties
Example Usage
Fixes the issue reported in the problem statement where generic types would fail to compile with the macro.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.