Skip to content

Instantly share code, notes, and snippets.

@ollieatkinson
Last active September 30, 2020 21:22
Show Gist options
  • Save ollieatkinson/9ede8b0e21f49c955542a46d74eccf52 to your computer and use it in GitHub Desktop.
Save ollieatkinson/9ede8b0e21f49c955542a46d74eccf52 to your computer and use it in GitHub Desktop.
Equatable for Any
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 }
}
}
@ollieatkinson
Copy link
Author

AnyHashable.isEqual([1], [1]) // true
AnyHashable.isEqual([1], 1) // false
AnyHashable.isEqual("test", 1) // false
AnyHashable.isEqual("test", "test") // true

@ollieatkinson
Copy link
Author

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