據我瞭解,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
是否'令x:(() - >無效) - >虛空= {F($ 0 )}'算作解決方案?這不是封閉,而是封裝。 –
@MartinR我想是的,是的。我發現奇怪的是,沒有(我知道)允許這個任務的類型關係 – Alexander
您可能有興趣知道編譯器實際上只會爲'rethrows'函數創建一個重載 - 一個'throws' 。如果將它應用於不會拋出的閉包,編譯器會簡單地減少函數返回錯誤的可能性。因此我懷疑沒有簡單的方法來獲得非投擲表單 - 封閉包裝可能是最好的解決方案。 – Hamish