Skip to content

Encode enum cases without associated values#158

Open
Amzd wants to merge 7 commits intoSwiftyLab:mainfrom
Amzd:main
Open

Encode enum cases without associated values#158
Amzd wants to merge 7 commits intoSwiftyLab:mainfrom
Amzd:main

Conversation

@Amzd
Copy link

@Amzd Amzd commented Feb 7, 2026

Fixes #157

This breaks some @IgnoreCodingInitialized tests but that API is too confusing for me. I have no idea what it is actually supposed to do, so I can't tell if the failing tests are actually breaking the intended use. A maintainer needs to look at that.

@soumyamahunt
Copy link
Contributor

This breaks some @IgnoreCodingInitialized tests but that API is too confusing for me. I have no idea what it is actually supposed to do, so I can't tell if the failing tests are actually breaking the intended use. A maintainer needs to look at that.

Can you provide what is the error you getting? The change seems to be in the right direction. So the tests might need to be updated with latest data.

generatedCode
}
} else {
preSyntax("\(values.first!)")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will cause unused variable warnings in case of your original example. Can you address that in someway? May be passing some flag to this preSyntax closure and customizing the output based on this flag?

Copy link
Author

Choose a reason for hiding this comment

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

I changed nothing in the decoding part and there there is also an unused variable so this seems like skewed standards but I have fixed this.

@Amzd
Copy link
Author

Amzd commented Feb 15, 2026

Can you provide what is the error you getting?

@Codable
@IgnoreCodingInitialized
enum SomeEnum {
    case bool(_ variableBool: Bool = true)
}

this previously encoded to nothing which means now all the tests fail with diff:

 extension SomeEnum: Encodable {
     func encode(to encoder: any Encoder) throws {
         var container = encoder.container(keyedBy: CodingKeys.self)
         switch self {
         case .bool(_: _):
+            let _ = container.superEncoder(forKey: CodingKeys.bool)
-            break

and it is unclear to me whether this is expected behavior from @IgnoreCodingInitialized or if the tests were just wrong.

@Amzd
Copy link
Author

Amzd commented Feb 15, 2026

I believe the previous behavior of @IgnoreCodingInitialized was a bug as I think on enums it is only meant to ignore the associated values, not the whole value. This PR fixes the bug but this might break implementations relying on that bug as they may have misinterpreted @IgnoreCodingInitialized to mean "ignore encoding completely". Thoughts are appreciated, if you say the fix is fine then I update the old tests.

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

Successfully merging this pull request may close these issues.

[Bug] enum cases are not encoded unless type specified

2 participants