Last active
September 30, 2020 21:22
-
-
Save ollieatkinson/9ede8b0e21f49c955542a46d74eccf52 to your computer and use it in GitHub Desktop.
Equatable for Any
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
extension Equatable { | |
public static func isEqual(_ l: Any, _ r: Any) -> Bool { | |
(l as? Self).map { isEqual($0, r) } ?? false | |
} | |
public static func isEqual(_ l: Self, _ r: Any) -> Bool { | |
isEqual(r, l) | |
} | |
public static func isEqual(_ l: Any, _ r: Self) -> Bool { | |
(l as? Self).map{ $0 == r } ?? false | |
} | |
} | |
extension Collection where Element == AnyHashable { | |
public static func are(equal first: Element, _ rest: Element...) -> Bool { | |
rest.allSatisfy { $0 == first } | |
} | |
} |
Author
ollieatkinson
commented
Sep 30, 2020
import Foundation
[AnyHashable].are(equal: 1, 2, 3) // false
[AnyHashable].are(equal: 1, 1, NSNumber(1)) // true
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment