Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
groue opened this issue Apr 26, 2024 · 0 comments
Open
Labels
feature A feature request or implementation Observation Area → standard library: The `Observation` module under the standard library umbrella

Comments

@groue
Copy link

groue commented Apr 26, 2024

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:

  • No one expects coding keys to be prefixed with underscores.
  • Adding @Observable to a working Codable type breaks its Codable facet, because it can no longer decode previously encoded data. This is pretty bad, actually, because this breaks the strong compatibility contract of Codable.

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)

@groue groue added feature A feature request or implementation triage needed This issue needs more specific labels labels Apr 26, 2024
@hborla hborla added Observation Area → standard library: The `Observation` module under the standard library umbrella and removed triage needed This issue needs more specific labels labels Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation Observation Area → standard library: The `Observation` module under the standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants