将重新抛出函数保存为非抛出闭包

Save rethrowing function as a non-throwing closure

据我了解,rethrows 本质上是从单个 declaration/definition 创建两个函数,如下所示:

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,有没有办法将它保存为 "non-throwing" 形式的闭包?假设是这样的:

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

直到有人提出更好的解决方案:使用包装器

func f(_ c: () throws -> Void) rethrows { try c() }

let x: (() -> Void) -> Void = { f([=10=]) }