Created
January 9, 2018 17:18
-
-
Save jakebromberg/77ffb421b2379efe0e7d21f0418bbd06 to your computer and use it in GitHub Desktop.
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 AnyEquatable: Equatable { | |
static func ==(lhs: AnyEquatable, rhs: AnyEquatable) -> Bool { | |
return lhs.isEqual(rhs.value) | |
} | |
private let isEqual: (Any) -> Bool | |
private let value: Any | |
init<T: Equatable>(_ value: T) { | |
self.value = value | |
isEqual = { ($0 as? T) == value } | |
} | |
} | |
let x1 = AnyEquatable(1) | |
let x2 = AnyEquatable(1) | |
assert(x1 == x2) | |
let y1 = AnyEquatable(1) | |
let y2 = AnyEquatable(2) | |
assert(y1 != y2) | |
let z1 = AnyEquatable(1) | |
let z2 = AnyEquatable("1") | |
assert(z1 != z2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment