Created
January 24, 2018 06:42
-
-
Save chriseidhof/15f2df3036ac4b3a5f69d49c58f19fd7 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
//: [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