As far as I understand, rethrows
essentially creates two functions from a single declaration/definition, like so:
func f(_ c: () throws -> Void) rethrows { try c()}
// has the same effect as declaring two seperate functions, with the same name:
func g(_ c: () throws -> Void) throws { try c() }
func g(_ c: () -> Void) { c() }
If I have a rethrowing function, like f
, is there way to save it as a closure in it's "non-throwing" form? Hypothetically, like so:
let x: (() -> Void) -> Void = f
// invalid conversion from throwing function of type '(() throws -> Void) throws -> ()' to non-throwing function type '(() -> Void) -> Void'
x{ print("test") } // "try" not necessary, because x can't throw
Until someone comes up with a better solution: Use a wrapper
func f(_ c: () throws -> Void) rethrows { try c() }
let x: (() -> Void) -> Void = { f($0) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With