<div dir="ltr">I&#39;ve wanted something like this as well. I think it would be harder than it seems, because &quot;x = 1&quot; might need to perform initialization, or assignment, depending how it&#39;s used.<div><br></div><div>It could make sense to have something like &quot;@noescape(executed_exactly_once)&quot; but this might be so limited it&#39;s not worth it. And I&#39;m not sure how it should interact with throws.</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Thu, Jan 28, 2016 at 11:38 PM, Gwendal Roué <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">Hello,<div><br></div><div>I’d like to discuss the opportunity to let functions declare that a closure argument is guaranteed to have been executed when the function has returned.</div><div><br></div><div>For example:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2"><span style="white-space:pre-wrap">        </span>func</span> f(<span style="color:#bb2ca2">@noescape</span>(<span style="color:#703daa">executed</span>) closure: () -&gt; ()) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>    closure()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div></div><div>The expected advantage is that the compiler would know that a variable set inside the closure is guaranteed to be initialized, and that it can be used after the execution of the function, as below:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="color:#bb2ca2"><span style="white-space:pre-wrap">        </span>let</span><span style="color:#000000"> x: </span><span style="color:#703daa">Int</span><span style="color:#000000">  </span>// Not initialized</div><div style="margin:0px;line-height:normal"><span style="color:#31595d"><span style="white-space:pre-wrap">        </span>f</span> { <span style="color:#4f8187">x</span> = <span style="color:#272ad8">1</span> }</div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="color:#3d1d81"><span style="white-space:pre-wrap">        </span>print</span><span style="color:#000000">(</span><span style="color:#4f8187">x</span><span style="color:#000000">)    </span>// Guaranteed to be initialized</div><div><br></div></div></div><div>Today developers have to write pessimistic code like below:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#bb2ca2"><span style="white-space:pre-wrap">        </span>var</span><span style="color:#000000"> x: </span><span style="color:#703daa">Int</span><span style="color:#000000"> = </span><span style="color:#272ad8">0</span><span style="color:#000000"> </span>// `var` declaration, with some irrelevant value</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#31595d"><span style="white-space:pre-wrap">        </span>f</span> { <span style="color:#4f8187">x</span> = <span style="color:#272ad8">1</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="white-space:pre-wrap">        </span>print<span style="color:#000000">(</span><span style="color:#4f8187">x</span><span style="color:#000000">)</span></div></div><div><span style="color:#000000"><br></span></div><div>As for a real world usage, I’d like to access a database in a safe (queued) way, and fetch values out of it:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2"><span style="white-space:pre-wrap">        </span>let</span> items: [<span style="color:#4f8187">Item</span>]</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2"><span style="white-space:pre-wrap">        </span>let</span> users: [<span style="color:#4f8187">User</span>]</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:#4f8187"><span style="white-space:pre-wrap">        </span>dbQueue</span><span style="color:#000000">.</span>inDatabase<span style="color:#000000"> { db </span><span style="color:#bb2ca2">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>    <span style="color:#4f8187">items</span> = <span style="color:#4f8187">Item</span>.all().fetchAll(db)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>    <span style="color:#4f8187">users</span> = <span style="color:#4f8187">Item</span>.all().fetchAll(db)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>}</div></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Gwendal Roué</div><div><br></div></font></span></div><br>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>