Skip to content

Instantly share code, notes, and snippets.

@helje5
Created August 2, 2023 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helje5/6827f34e9a68951a850faee94f6c4d8d to your computer and use it in GitHub Desktop.
Save helje5/6827f34e9a68951a850faee94f6c4d8d to your computer and use it in GitHub Desktop.
Compare Any w/ Swift 5.7
func isEqual(_ lhs: Any, _ rhs: Any) -> Bool {
guard let lhs = lhs as? any Equatable else { return false }
func isEqual<T: Equatable>(lhs: T, rhs: Any) -> Bool {
guard let rhs = rhs as? T else { return false }
return lhs == rhs
}
return isEqual(lhs: lhs, rhs: rhs)
}
print("5==5", isEqual(5, 5)) // true
print("5==6", isEqual(5, 6)) // false
print("'Hello'==6", isEqual("Hello", 6)) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment