The paragraph after defining ifelseThrows
states the following (emphasis mine):
This code works, but the situation is worse than initially described. Suppose only the first expression throws or suppose only the second throws. Do you need to make four versions of the same function to handle all the cases? Because the keyword throws does not figure into the signature of a Swift function, you would need to have four flavors of
ifelse
, all with slightly different names.
Because the keyword throws does not figure into the signature of a Swift function
However, throws
does have an impact on a function’s type signature. That is, all non-throwing functions can be case as a throwing function:
func convert<A, B>(_ f: @escaping (A) -> B) -> (A) throws -> B {
f
}
This function compiles and so (A) -> B
is a subtype of (A) throws -> B
.
you would need to have four flavors of ifelse
, all with slightly different names
As the above convert
function shows, we can call ifelseThrows
with non-throwing functions. However, throws
requires try
no matter what, but rethrows
does not.