Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created January 24, 2018 06:42
Show Gist options
  • Save chriseidhof/15f2df3036ac4b3a5f69d49c58f19fd7 to your computer and use it in GitHub Desktop.
Save chriseidhof/15f2df3036ac4b3a5f69d49c58f19fd7 to your computer and use it in GitHub Desktop.
//: [Previous](@previous)
import Foundation
struct AnyEquatable {
private let isEqualTo: (Any) -> Bool
let value: Any
init<A: Equatable>(_ value: A) {
self.value = value
isEqualTo = { other in
guard let o = other as? A else { return false }
return value == o
}
}
}
extension AnyEquatable: Equatable {
static func ==(lhs: AnyEquatable, rhs: AnyEquatable) -> Bool {
return lhs.isEqualTo(rhs.value)
}
}
let x = AnyEquatable(5)
let y = AnyEquatable("hello")
x == y
AnyEquatable(4) == AnyEquatable(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment