<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="">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="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func</span> f(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">@noescape</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">executed</span>) closure: () -> ()) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span> closure()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </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="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// Not initialized</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>f</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </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="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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="font-variant-ligatures: no-common-ligatures; color: #31595d" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>f</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span> = <span style="font-variant-ligatures: no-common-ligatures; 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 class="Apple-tab-span" style="white-space:pre">        </span>print<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">x</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><br class=""></span></div><div class="">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 class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let</span> items: [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Item</span>]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let</span> users: [<span style="font-variant-ligatures: no-common-ligatures; 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="font-variant-ligatures: no-common-ligatures; color: #4f8187" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>dbQueue</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>inDatabase<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { db </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">items</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Item</span>.all().fetchAll(db)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">users</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Item</span>.all().fetchAll(db)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div></div><div class=""><br class=""></div><div class="">Gwendal Roué</div><div class=""><br class=""></div></body></html>