<div dir="ltr">-1. Much of the utility of `defer` is that it's guaranteed to execute. As you demonstrate, it's possible to capture a flag and choose not to do something within the defer block itself. We don't need sugar for that.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 7, 2017 at 12:20 PM, Rien via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Is there any interest in a proposal to introduce a named defer statement that can be cancelled?</div><div><br></div><div>Lately I find myself writing this kind of code:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">func</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> openFile(kind: </span><span style="font-variant-ligatures:no-common-ligatures">String</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">) -> </span><span style="font-variant-ligatures:no-common-ligatures">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"><</span><span style="font-variant-ligatures:no-common-ligatures">FILE</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">>? {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">var</span><span style="font-variant-ligatures:no-common-ligatures"> file = </span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81">fopen</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b">"MyFile.txt"</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b">"r"</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">var</span><span style="font-variant-ligatures:no-common-ligatures"> closeFile = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">defer</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">if</span><span style="font-variant-ligatures:no-common-ligatures"> closeFile { </span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81">fclose</span><span style="font-variant-ligatures:no-common-ligatures">(file) } }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">fileIsNotValid</span><span style="font-variant-ligatures:no-common-ligatures">(file) { </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">nil</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">fileDoesNotContainsData</span><span style="font-variant-ligatures:no-common-ligatures">(file, kind) { </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">nil</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">fileDataOutOfDate</span><span style="font-variant-ligatures:no-common-ligatures">(file) { </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">nil</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures">// Prevent the deferred handler from closing the file</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> closeFile = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">false</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> file</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>Which imo would be much cleaner if we were able to write:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> openFile(kind: </span><span style="font-variant-ligatures:no-common-ligatures">String</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">) -> </span><span style="font-variant-ligatures:no-common-ligatures">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><</span><span style="font-variant-ligatures:no-common-ligatures">FILE</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">>? {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> file = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">fopen</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">"MyFile.txt"</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">"r"</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> CLOSE_FILE: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">defer</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">fclose</span><span style="font-variant-ligatures:no-common-ligatures">(<wbr>file) }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">fileIsNotValid</span><span style="font-variant-ligatures:no-common-ligatures">(file) { </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">nil</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">fileDoesNotContainsData</span><span style="font-variant-ligatures:no-common-ligatures">(<wbr>file, kind) { </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">nil</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">fileDataOutOfDate</span><span style="font-variant-ligatures:no-common-ligatures">(file) { </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">nil</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_-3136840737274756464webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">// Prevent the deferred handler from closing the file</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="color:rgb(187,44,162)">cancel</span><span style="font-variant-ligatures:no-common-ligatures"> CLOSE_FILE</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> file</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>Regards,<br>Rien<br><br>Site: <a href="http://balancingrock.nl" target="_blank">http://balancingrock.nl</a><br>Blog: <a href="http://swiftrien.blogspot.com" target="_blank">http://swiftrien.blogspot.com</a><br>Github: <a href="http://github.com/Swiftrien" target="_blank">http://github.com/Swiftrien</a><br>Project: <a href="http://swiftfire.nl" target="_blank">http://swiftfire.nl</a><br></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>