Skip to content

Instantly share code, notes, and snippets.

@jakebromberg
Created January 9, 2018 17:18
Show Gist options
  • Save jakebromberg/77ffb421b2379efe0e7d21f0418bbd06 to your computer and use it in GitHub Desktop.
Save jakebromberg/77ffb421b2379efe0e7d21f0418bbd06 to your computer and use it in GitHub Desktop.
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