Created
February 25, 2019 03:16
-
-
Save joshuajhomann/33b6c0eb588f5678f089a3535f7610e1 to your computer and use it in GitHub Desktop.
Equatable Key Paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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