I recently encountered a couple of interesting behaviors around rethrows checking.
I tried writing something like:
func f(_ k: (() throws -> ())? = nil) rethrows {
if let k_ = k {
try k_() // error: call can throw, but the error is not handled; a function declared 'rethrows' may only throw if its parameter does
}
}
Ok, the compiler can't reason that k_'s throwing-ness is connected to k's throwing-ness (not an unreasonable restriction, since reasoning about arbitrary types apart from Optional could be messy/impossible).
Since that's an error, I tried making it a no-op by default to work around it.
func f(_ k: () throws -> () = { }) rethrows {
try k()
}
let x = f()
// error: call can throw but is not marked with 'try'
// note: call is to 'rethrows' function, but a defaulted argument function can throw
To some extent, I do understand why we'd want to not treat the default argument type as something different from the parameter type.
I don't really have a question, I just figured these were a couple of interesting edge cases, so I'd share them.
I agree that these are frustrating edge cases, and I don’t have anything to add there. But if you’re looking for a way to rethrow based on an optional function, you can use the k?() notation: