Ordered collection diffing

Associated changes give the diff type the ability to express moves:

let diff = [0, 1, 2].difference(from:[2, 0, 1])

print(diff)
// OrderedCollectionDifference<Int>(
//     insertions: [Diffing.OrderedCollectionDifference<Swift.Int>.Change.insert(offset: 2, element: 2, associatedWith: nil)],
//     removals: [Diffing.OrderedCollectionDifference<Swift.Int>.Change.remove(offset: 0, element: 2, associatedWith: nil)]
// )

print(diff.inferringMoves())
// OrderedCollectionDifference<Int>(
//     insertions: [Diffing.OrderedCollectionDifference<Swift.Int>.Change.insert(offset: 2, element: 2, associatedWith: Optional(0))],
//     removals: [Diffing.OrderedCollectionDifference<Swift.Int>.Change.remove(offset: 0, element: 2, associatedWith: Optional(2))]
// )

Associations aren't widely used outside inferringMoves() in this proposal but they enable functionality that's useful to adopters and future proposals, like a function that records a collection's mutations (using associations to track move and replace operations), or a UI that uses associations to determine what animations it should use when applying changes.

1 Like