<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On Jan 29, 2016, at 12:23 AM, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">I've wanted something like this as well. I think it would be harder than it seems, because "x = 1" might need to perform initialization, or assignment, depending how it's used.<div class=""><br class=""></div><div class="">It could make sense to have something like "@noescape(executed_exactly_once)" but this might be so limited it's not worth it. And I'm not sure how it should interact with throws.</div></div></div></blockquote><div><br class=""></div><div>I think that something like this is implementable, and making it a modifier to @noescape is sensible.</div><div><br class=""></div><div>The semantics we could support is that the function is guaranteed to call the closure exactly once on any path that could lead to a return or throw.</div><div><br class=""></div><div>This approach allows you to pass the closure down the stack, and composes with error handling. &nbsp;It is obviously limited what you can do with the closure, but that is necessary to validate correctness.</div><div><br class=""></div><div>-Chris</div><div><br class=""></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class="">Jacob<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote">On Thu, Jan 28, 2016 at 11:38 PM, Gwendal Roué <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Hello,<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">For example:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class=""><span style="white-space:pre-wrap" class="">        </span>func</span> f(<span style="color:#bb2ca2" class="">@noescape</span>(<span style="color:#703daa" class="">executed</span>) closure: () -&gt; ()) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class="">        </span>&nbsp; &nbsp; closure()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class="">        </span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""></div></div><div class="">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 class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><div style="margin:0px;line-height:normal;color:rgb(0,132,0)" class=""><span style="color:#bb2ca2" class=""><span style="white-space:pre-wrap" class="">        </span>let</span><span style="" class=""> x: </span><span style="color:#703daa" class="">Int</span><span style="" class="">&nbsp; </span>// Not initialized</div><div style="margin:0px;line-height:normal" class=""><span style="color:#31595d" class=""><span style="white-space:pre-wrap" class="">        </span>f</span> { <span style="color:#4f8187" class="">x</span> = <span style="color:#272ad8" class="">1</span> }</div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)" class=""><span style="color:#3d1d81" class=""><span style="white-space:pre-wrap" class="">        </span>print</span><span style="" class="">(</span><span style="color:#4f8187" class="">x</span><span style="" class="">)&nbsp; &nbsp; </span>// Guaranteed to be initialized</div><div class=""><br class=""></div></div></div><div class="">Today developers have to write pessimistic code like below:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="color:#bb2ca2" class=""><span style="white-space:pre-wrap" class="">        </span>var</span><span style="" class=""> x: </span><span style="color:#703daa" class="">Int</span><span style="" class=""> = </span><span style="color:#272ad8" class="">0</span><span style="" class=""> </span>// `var` declaration, with some irrelevant value</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#31595d" class=""><span style="white-space:pre-wrap" class="">        </span>f</span> { <span style="color:#4f8187" class="">x</span> = <span style="color:#272ad8" class="">1</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)" class=""><span style="white-space:pre-wrap" class="">        </span>print<span style="" class="">(</span><span style="color:#4f8187" class="">x</span><span style="" class="">)</span></div></div><div class=""><span style="" class=""><br class=""></span></div><div class="">As for a real&nbsp;world usage, I’d like to access a database in a safe (queued) way, and fetch values out of it:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class=""><span style="white-space:pre-wrap" class="">        </span>let</span> items: [<span style="color:#4f8187" class="">Item</span>]</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class=""><span style="white-space:pre-wrap" class="">        </span>let</span> users: [<span style="color:#4f8187" class="">User</span>]</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)" class=""><span style="color:#4f8187" class=""><span style="white-space:pre-wrap" class="">        </span>dbQueue</span><span style="" class="">.</span>inDatabase<span style="" class=""> { db </span><span style="color:#bb2ca2" class="">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class="">        </span>&nbsp; &nbsp; <span style="color:#4f8187" class="">items</span> = <span style="color:#4f8187" class="">Item</span>.all().fetchAll(db)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class="">        </span>&nbsp; &nbsp; <span style="color:#4f8187" class="">users</span> = <span style="color:#4f8187" class="">Item</span>.all().fetchAll(db)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class="">        </span>}</div></div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Gwendal Roué</div><div class=""><br class=""></div></font></span></div><br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>