-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Observation] Switch Observable
to be a non-marker protocol
#66993
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
[Observation] Switch Observable
to be a non-marker protocol
#66993
Conversation
With support for redundant conformance declarations via macros, the `Observable` protocol can be a non-marker protocol, which provides more flexibility for evolution in the future. rdar://111463883
@swift-ci Please smoke test |
I'm not sure how making it a non-marker (ABI) protocol makes it more flexible in the future. There are no requirements nor could we add them later without default implementations. Any requirement would need to expose the registrar which doing so would leak internal details. |
@swift-ci Please test |
This change switch to implement the new ExtensionMacro protocol, the requirement for which includes information about whether the conformance to the Observable protocol has already been added, either in the declaration or in a superclass to the macro-attributed type. This allows the @observable macro to be applied to subclasses of observable types without redundant-conformance errors.
@swift-ci Please test |
ee5df1e
to
bfee080
Compare
@swift-ci Please smoke test |
umm I disagree with the merge here: my objection was not at all addressed. The implementation did not seem to address the subclassing issue, and the justification of the non-marker protocol seemed to be hand-wavy/nebulous. |
This was discussed in detail in the Language Steering Group; the most general point is that the idea of needing to "justify a non-marker protocol" is precisely backwards; all protocols should be non-marker protocols unless there is a specific need for them to be marker protocols, and that does not exist in this case. (It was previously necessary to workaround the interaction of macros with protocol conformance and subclassing, as you well know, but we addressed that by fixing how the compiler handles conformances created by macros.) |
…ang#66993) With support for redundant conformance declarations via macros, the `Observable` protocol can be a non-marker protocol, which provides more flexibility for evolution in the future. rdar://111463883 This change also switches to the new ExtensionMacro protocol, the requirement for which includes information about whether the conformance to the Observable protocol has already been added, either in the declaration or in a superclass to the macro-attributed type. This allows the @observable macro to be applied to subclasses of observable types without redundant-conformance errors.
@natecook1000 Why do you need the Observable protocol in the first place? It seems to be unused, as well as the |
* Make the `@Observable` macro class only (#67033) * Make ObservationRegistrar Codable/Hashable These conformances enable automatic Codable synthesis for Observable types, and smooth the runway for structs being supported by the Observable macro in the future. * Limit Observable macro to classes This removes the ability for the Observable macro to apply to structs, and adds diagnostic tests for the three disallowed declaration kinds. * [Observation] Switch `Observable` to be a non-marker protocol (#66993) With support for redundant conformance declarations via macros, the `Observable` protocol can be a non-marker protocol, which provides more flexibility for evolution in the future. rdar://111463883 This change also switches to the new ExtensionMacro protocol, the requirement for which includes information about whether the conformance to the Observable protocol has already been added, either in the declaration or in a superclass to the macro-attributed type. This allows the @observable macro to be applied to subclasses of observable types without redundant-conformance errors.
@DmT021 Even though the |
Yes, I understand that this protocol provides semantics. But is it useful? I mean it doesn't help us to build a compile-time or runtime check that the properties of the objects we want to observe are observable. And programmers also do not see the list of conformances for each object when using their properties in the |
yes; it helps to ensure that usage scopes: such as Now it definitely could exist without it - but when the other use cases are considered (specifically integration with other layers, either SwiftUI or developer created libraries) it makes sense to have some sort of identification that the types are actually observable. This was a short-coming of KVO. There was no way to tell if a type has adopted it or not. This at least lets the developer using the type know that it was considered. |
@phausler Ok, thanks a lot for the detailed answer. And what about the |
With support for redundant conformance declarations via macros, the
Observable
protocol can be a non-marker protocol, which provides more flexibility for evolution in the future.Note: This requires a change that would allow macros to emit redundant conformances. Without that change, this will fail tests that use the
@Observable
macro on both a class and its subclass (e.g.Entity
and its subclasses here).rdar://111463883