Another interesting future direction we could take to address polymorphism over throws
specifically would be to adopt typed throws
, and make the error type an independent type argument of all function types. This would mean that a type that doesn't throw effectively throws Never
, and one that throws
without a type by default throws Error
:
(X, Y, Z) -> W === (X, Y, Z) throws Never -> W
(X, Y, Z) throws -> W === (X, Y, Z) throws Error -> W
This would allow protocols to be generic over throwing and nonthrowing implementations by making the error a separate associated type:
protocol MyIterator {
associatedtype Element
associatedtype Error: Swift.Error
mutating func next() throws Error -> Element
}
struct NonthrowingImplementation: MyIterator {
mutating func next() -> Int // conforms with Error == Never
}
struct ThrowingImplementation: MyIterator {
mutating func next() throws -> Int // conforms with Error == Swift.Error
}