I was trying out Swift 6 to play with its new features like the Typed throws introduced with SE-0413, and ran into an unexpected compiling error when using the typed throws closure as instance properties. Here is the code:
enum TypedError: Error {
case failed
}
struct Example {
var fail: () async throws(TypedError) -> Void
}
let example = Example(fail: { throw TypedError.failed })
// error: Invalid conversion of thrown error type 'any Error' to 'TypedError'
And the error occurred at the last line saying:
Invalid conversion of thrown error type 'any Error' to 'TypedError'
This could break some code that depends on the precisely inferred type. To prevent this from becoming a source compatibility problem, we apply the same rule as for do...catch statements to limit inference: throw statements within the closure body are treated as having the type any Error in Swift 5.
Have you either opted into the Swift 6 language mode (it's not the default when you use the Swift 6.0 compiler) or enabled the upcoming feature flag FullTypedThrows?
There are problems with implicit typing. The Swift 6 Language Mode does not help. But so far, I've found that you can get around them with explicitness.
let example = Example { () throws(TypedError) in throw .failed }
IIUC, it should be an either or situation: either you build in Swift 5 mode with the experimental (it doesn't show up in Xcode 16's upcoming toggles) flag or in Swift 6 mode with no flags.