据我了解,rethrows
基本上从单个声明/定义创建了两个函数,如下所示:
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() }
如果我有一个重新抛出函数,比如f
,有没有办法将它保存为“非投掷”形式的闭包?假设,就像这样:
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
答案 0 :(得分:3)
在有人提出更好的解决方案之前:使用包装器
func f(_ c: () throws -> Void) rethrows { try c()}
let x: (() -> Void) -> Void = { f($0) }