pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
11.91k stars 1.37k forks source link

Add conditional Hashable conformance to Shared #3155

Closed Jackstone92 closed 1 month ago

Jackstone92 commented 1 month ago

Here's a small PR that adds conditional Hashable conformance to Shared in a similar fashion to BindingState.

stephencelis commented 1 month ago

@Jackstone92 Thanks for taking the time to PR!

Unfortunately the @Shared type cannot be conditionally hashable because it's reference-y, and that means its hash could change over time. We document this as a "gotcha" here: https://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/sharingstate/#Hashability

One example problem is that if you used a Shared value as a key in a dictionary, and its hash changed after insertion, the dictionary would be in an invalid state, which I believe would lead to a precondition/crash.

Generally reference-y things are hashable by their object identifier, but we can't do that here, either, because the equatable conformance would then be wrong. So we're kinda stuck here.

Please let us know the scenario you ran into this problem, though! Maybe there are other solutions to it.

Jackstone92 commented 1 month ago

Ah yeah that is a "gotcha"! Thanks, will close this and keep digging!