Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created June 7, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanKeen/01c7d691a5f96b6377b4b86401b039d8 to your computer and use it in GitHub Desktop.
Save IanKeen/01c7d691a5f96b6377b4b86401b039d8 to your computer and use it in GitHub Desktop.
AnyEquatable
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