swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.57k stars 10.35k forks source link

The Observable macro does not play well with the standard Codable protocol #73280

Open groue opened 6 months ago

groue commented 6 months ago

Motivation

Hello,

When a developer declares a type both @Observable and Codable, there is a bad surprise: the Observable macro transforms regular properties into computed ones, and adds new properties, prefixed by underscores. Codable then only deals with underscored properties.

In the end, the type gets a "corrupted" Codable facet due to Observable:

I was aware of this problem from two sources:

In both cases, developers were completed puzzled.

And workarounds are not pretty. Basically they ruin the most valuable aspect of Codable, which is the convenience of compiler-synthesized code.

Please consider solving this issue. Standard components such as @Observable and Codable should not conflict together.

Proposed solution

I don't have any ready-made solution. I expect that we'd need to give much more powers to macros.

Alternatives considered

No response

Additional information

I submitted the same report as FB13756604 (The Observable macro does not play well with the standard Codable protocol)

ConfusedVorlon commented 3 months ago

lol - I actually have found this!

I'm using explicit conformance with the excellent codextended