Using Swift 6 and typed throws, I've noticed that this works:
func foobar<Failure: Error>(closure: () throws(Failure) -> Void) throws(Failure) {}
So does this:
protocol FoobarProtocol {
associatedtype Failure: Error
func foobar(closure: () throws(Failure) -> Void) throws(Failure)
}
But this doesn't work:
protocol FoobarProtocol {
func foobar<Failure: Error>(closure: () throws(Failure) -> Void) throws(Failure)
}
Compilation fails with the following error:
<stdin>:2:75: error: cannot find type 'Failure' in scope
1 | protocol FoobarProtocol {
2 | func foobar<Failure: Error>(closure: () throws(Failure) -> Void) throws(Failure)
| `- error: cannot find type 'Failure' in scope
3 | }
<stdin>:2:75: error: thrown type '<<error type>>' does not conform to the 'Error' protocol
1 | protocol FoobarProtocol {
2 | func foobar<Failure: Error>(closure: () throws(Failure) -> Void) throws(Failure)
| `- error: thrown type '<<error type>>' does not conform to the 'Error' protocol
3 | }
If I remove the second throws(Failure)
so that only the closure is throwing and not the function itself, it builds fine. rethrows
keyword works too, which also does what I intended to do here.
Is that normal? Is this supposed to work, or are we expected to use associated types in protocols for typed throws? This looks like a Swift bug to me.
Thanks!