You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On Apple platforms, after importing Foundation, AnyHashable does Obj-C bridging the way AnyObject does, which means it does NSNumber bridging rules for numeric casts. For example:
Also worth noting that on macOS, if you don’t import Foundation then AnyHashable doesn’t handle casts this way (it returns nil like Linux’s current behavior).
Since apple/swift#17396 AnyHashable has its own parallel reimplementation of Foundation's equality semantics, so that it can correctly implement Hashable.
Unfortunately, this mechanism doesn't currently extend to downcasting – as?-casts through AnyHashable between "equal" values of different types only works on Objective-C runtimes and only when Foundation is loaded.
For primitive types that bridge with NSNumber, we can implement cross-casting by simply checking for all supported target types.
This approach would not work for collections, though:
[1, 2, 3] as [Int] as AnyHashable as? [Double] // non-nil on Darwin if Foundation is loaded
[[1, 2, 3]] as Set<[Int]> as AnyHashable as? Set<[Double]>
etc.
Environment
Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Swift version 4.2.3 (swift-4.2.3-RELEASE)
Target: x86_64-unknown-linux-gnu
Additional Detail from JIRA
md5: 3762a595cf0c814bdbe714ab92679f7b
Issue Description:
On Apple platforms, after importing Foundation,
AnyHashable
does Obj-C bridging the wayAnyObject
does, which means it doesNSNumber
bridging rules for numeric casts. For example:On Linux (tested using the official swift 4.2 Docker image) this doesn't work.
NSNumber
bridges properly butAnyObject
doesn't.The text was updated successfully, but these errors were encountered: