Created
June 7, 2022 16:35
-
-
Save IanKeen/01c7d691a5f96b6377b4b86401b039d8 to your computer and use it in GitHub Desktop.
AnyEquatable
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
public struct AnyEquatable: Equatable { | |
public let base: Any | |
private let isEqual: (_ other: Any) -> Bool | |
public init<T: Equatable>(_ value: T) { | |
self.base = value | |
self.isEqual = { other in | |
guard let other = other as? T else { return false } | |
return other == value | |
} | |
} | |
public static func ==(lhs: AnyEquatable, rhs: AnyEquatable) -> Bool { | |
return lhs.isEqual(rhs.base) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment