Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
Created February 25, 2019 03:16
Show Gist options
  • Save joshuajhomann/33b6c0eb588f5678f089a3535f7610e1 to your computer and use it in GitHub Desktop.
Save joshuajhomann/33b6c0eb588f5678f089a3535f7610e1 to your computer and use it in GitHub Desktop.
Equatable Key Paths
struct AnyEquatableKeyPath<Root> {
let keyPath: PartialKeyPath<Root>
let isEqual: (Root, Root) -> Bool
init<Value: Equatable>(_ keyPath: KeyPath<Root, Value>) {
self.keyPath = keyPath
self.isEqual = {left, right in
return left[keyPath: keyPath] == right[keyPath: keyPath]
}
}
}
extension Equatable {
func isEqualTo(other: Self, byComparing keyPaths:AnyEquatableKeyPath<Self>...) -> Bool {
return keyPaths.allSatisfy { $0.isEqual(self, other) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment