Hi, I'm curious why Observation doesn't do an equivalence check on the Equatable type.
One of the pain points about Published is that when the value being set doesn't change, it results in a lot of invalid updates attempts, will Observation optimize for the Equatable?
Because the macro has no knowledge about the type (this is a limitation of macros in general) the only way to determine the equitability is to hold the previous value and at runtime check if the type is equatable (via some really ugly hacks) and compare it (with even more ugly hacks). This opening routine is rather costly. From measurements this was not worth it; effectively it erased the gains of just observing the things that were used.
This has much more impact for the production of specific property observation; from the Combine perspective that is the subscription to the publisher of the @Published. That can, if done carefully, be done at compile time without those costs.
Given how @Observable works, the update will be triggered, but the theoretical == check triggered before exposing the new value would return false for b, meaning the update would always happen regardless of whether deduplication was active. In that example only a would be deduplicated. This could potentially point to the need for a separate protocol that allows this sort of observable deduplication without having to customize a type's Equatable conformance, but it's unclear whether that's really necessary yet.
Or if swift needs to change the withMutation to this form, then developers can add their own variants withMutation for Equatable, Hashable, Identifiable....
Yes, based on the current Observable, updates will be triggered anyway. However, if we optimize it by performing equality checks on Equatable types, then when foo.b changes, the update will not be triggered.
That's not a bad approach, it definitely resolves the problem from what I can tell without the hacks that would be needed else wise. It would need to be explored on what the ramifications of changing the emissions would be - well worth the investigation.
If you end up opening up a PR to change the macro code generation, tag me on it and we can iterate through a potential path to getting this fixed.