<div dir="ltr"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">I have been using defer for very happy. It’ like with keyword on python, using easily for needs closing context like NSFileHandle.</span><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">anyway defer is very useful statement for function scope.</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">However, I thinking now, What if i can use more small defer.</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">below, that’s codes of idea.</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">// firstbly, create Deferable protocol for decorating.</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">protocol Deferable {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    func finish()</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px">// secondly, extension NSFileHandle for deferable</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px">extension NSFileHandle: Deferable {}<br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">extension Deferable {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    func finish() {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        switch self {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        case let fh as NSFileHandle:</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">            fh.closeFile()</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        default:</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">            break</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px">// i have no skill create keyword for swift. so i make custom operator</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">infix operator !! {}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">func !! &lt;D: Deferable&gt;(lhs: D, rhs:(target: D)-&gt;Void) -&gt; D{</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    defer {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        lhs.finish()</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        print(&quot;finished&quot;)</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    rhs(target: lhs)</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    return lhs</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px">// for testing</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">class Test {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    func test() {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        NSFileHandle(forReadingAtPath: path)! !! {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">            print(&quot;read \($0.readDataOfLength(10))&quot;)</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">        }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(255,255,255)">}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(255,255,255);min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">Test().test()</div></div><div style="font-size:14px;color:rgb(0,0,0);margin:0px;line-height:normal;font-family:Menlo"><br></div><div style="font-size:14px;color:rgb(0,0,0);margin:0px;line-height:normal;font-family:Menlo">-&gt; </div><div style="color:rgb(0,0,0);font-size:12px;margin:0px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal"><b>read &lt;blah blah...&gt;</b></div><div style="margin:0px;line-height:normal"><b>finished</b></div><div style="margin:0px;line-height:normal"><b><br></b></div><div style="margin:0px;line-height:normal"><b>i have no idea this is how to useful.</b></div><div style="margin:0px;line-height:normal"><b>but it is make me more funny programming on swift.</b></div><div style="margin:0px;line-height:normal"><b><br></b></div><div style="margin:0px;line-height:normal"><b>thankyou </b></div></div></div>