I have stumbled upon this when upgrading to swift 4.2. I'm puzzled on what's going on here and what should happen :)
import Foundation
let data = [String: Any]()
func resolve<T>() -> T? {
return data["123"] as? T
}
if let result = resolve() as Any? {
print(result) // Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1)
} else {
print("empty") // Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)
}
Looks like a bug for me, because if you try to use value of result
on swift 4.2 you'll end up with fatalError
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x0000000113562b66 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x000000011359c080 libsystem_pthread.dylib`pthread_kill + 333
frame #2: 0x000000011330fc45 libsystem_c.dylib`abort + 127
frame #3: 0x00000001127f9c35 libswiftCore.dylib`swift::fatalError(unsigned int, char const*, ...) + 149
frame #4: 0x00000001127f0462 libswiftCore.dylib`swift::swift_dynamicCastFailure(void const*, char const*, void const*, char const*, char const*) + 82
frame #5: 0x00000001127f04d0 libswiftCore.dylib`swift::swift_dynamicCastFailure(swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, char const*) + 96
frame #6: 0x00000001127f52db libswiftCore.dylib`_dynamicCastToExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetExistentialTypeMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 1387
Could anyone advise?