DifferenceKit icon indicating copy to clipboard operation
DifferenceKit copied to clipboard

Fix the default implementation of Differentiable for Hashable

Open yunhao opened this issue 5 years ago • 1 comments

Checklist

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.

yunhao avatar Sep 11 '20 06:09 yunhao

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.

guidedways avatar Sep 30 '21 04:09 guidedways