<div dir="ltr">Any cancelable defer addition we could come up with will need a flag/state to indicate so, which won&#39;t be that much different from what you wrote. <div><br></div><div>Having said that, for your example may I suggest this approach instead:</div><div><br></div><div><p style="margin:0px;font-size:14px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> openFile(kind: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FILE</span><span style="font-variant-ligatures:no-common-ligatures">&gt;? {</span></p>
<p style="margin:0px;font-size:14px;line-height:normal;font-family:menlo;min-height:16px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:14px;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 = fopen(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;MyFile.txt&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;r&quot;</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-size:14px;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)">guard</span><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">fileIsValid</span><span style="font-variant-ligatures:no-common-ligatures">(file)</span></p>
<p style="margin:0px;font-size:14px;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(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">fileContainsData</span><span style="font-variant-ligatures:no-common-ligatures">(file, kind)</span></p>
<p style="margin:0px;font-size:14px;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(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">!</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)">else</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:14px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">        fclose(file)</span></p>
<p style="margin:0px;font-size:14px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:14px;line-height:normal;font-family:menlo;min-height:16px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:14px;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></p>
<p style="margin:0px;font-size:14px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 7, 2017 at 7:20 PM, Rien via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</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">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">&lt;</span><span style="font-variant-ligatures:no-common-ligatures">FILE</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">&gt;? {</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_-2607921745366353626webkit-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">&quot;MyFile.txt&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#d12f1b">&quot;r&quot;</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_-2607921745366353626webkit-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_-2607921745366353626webkit-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_-2607921745366353626webkit-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_-2607921745366353626webkit-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)">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">&lt;</span><span style="font-variant-ligatures:no-common-ligatures">FILE</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">&gt;? {</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_-2607921745366353626webkit-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)">&quot;MyFile.txt&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;r&quot;</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_-2607921745366353626webkit-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_-2607921745366353626webkit-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_-2607921745366353626webkit-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><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div style="color:rgb(34,34,34)">++++++++++++++++++++++++++</div><div style="color:rgb(34,34,34)">Diego Torres.</div><div style="color:rgb(34,34,34)">Phone (Mobile Germany): +49 157 30070985</div><div style="color:rgb(34,34,34)">Phone (Landline Chile): +56 2 29790978</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">Web: <a href="http://dtorres.me/" target="_blank">dtorres.me</a></div></div>
</div>