Fix the default implementation of Differentiable for Hashable
Checklist
- [x] All tests are passed.
- [ ] Added tests.
- [x] Documented the code using Xcode markup.
- [x] Searched existing pull requests for ensure not duplicated.
Description
The default implementation of Differentiabe for Hashable has a differenceIdentifier property. The implementation of this propeprty now return hashValue instead of self.
Related Issue
// Model
struct ElementModel: Hashable, Differentiable {
var id: Int
var title: String
var value: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
// Changeset
let list1 = [ElementModel(id: 1, title: "color", value: "red")]
let list2 = [ElementModel(id: 1, title: "color", value: "blue")]
let changeset = StagedChangeset(source: list1, target: list2)
changeset.forEach { print($0) } // two stage: `delete` -> `insert`
The above code produces a delete -> insert change, however, it should be a update change.
#114: Unexpected behavior when using the default implementations of Differentiable for Hashable
Motivation and Context
Close #114
Impact on Existing Code
If the existing code rely on the default implementation of Differentiable for Hashable, the reload behavior may changes. Some delete -> insert changes will become update changes.
Thanks for putting this in - I also think this is how it should work with Hashable. The whole point of differenceIdentifier is to uniquely identify an object. Returning self goes against the idea especially when both instances point to the same underlying object.
Would be nice to have this merged with the documentation reflecting the change.