<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><div><br><br>Sent from my iPad</div><div><br>On Jun 5, 2016, at 8:43 PM, Andrew Bennett &lt;<a href="mailto:cacoyi@gmail.com">cacoyi@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Thanks Matthew, my responses are inline:<br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 6, 2016 at 10:32 AM, Matthew Johnson <span dir="ltr">&lt;<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br><br>Sent from my iPad</div><span class=""><div><br>On Jun 5, 2016, at 6:50 PM, Andrew Bennett &lt;<a href="mailto:cacoyi@gmail.com" target="_blank">cacoyi@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Perhaps I was unclear, in my explanation.&nbsp;The guarantee I'm enforcing is that the closure is called exactly once before being released.<div><br></div><div>Everything I suggested is a compile-time check.</div><div><br></div><div>The compile-time warning and runtime `fatalError` I suggested could be replaced with a compile-time error, however even in this case it is still statically checked for the warning.</div><div><br></div>The compiler can statically guarantee <b>exactly one</b> of these things happens in methods using the closure:<br><div><div><ul><li>the closure is <b>called</b></li><li>the closure is <b>stored</b></li><li>the closure is <b>passed</b> to another method</li><li>the program&nbsp;<b>aborts</b> with something like a fatalError</li></ul><div>If the closure is stored then there must be a <b>deinit</b>, and those checks apply there as well.</div><div><br></div><div>I believe this is sufficient to ensure the closure is called once. Please let me know if there are any cases these checks miss.</div></div></div></div></div></blockquote><div><br></div></span><div>If the closure is stored in a member it could be called in the implementation of any other member.&nbsp; Calls to other members could come from arbitrary locations in the surrounding program at arbitrary points in time (unless you have static analysis that can prove a narrower set of possibilities).</div></div></blockquote><div>&nbsp;</div><div>This isn't a problem if the member has to have the type annotation, all uses of the member, whether from elsewhere in the program, or other methods, would have to pass the checks.</div><div><br></div><div>If you call the closure you must nil/replace the member.</div></div></div></div></div></blockquote><div><br></div>Ok, so the member must be an optional? &nbsp;If that is the idea I would suggest considering something similar to 'weak' where it automatically gets set to nil after it is called (which could maybe become a property behavior in the future).</div><div><br></div><div>I don't think you mentioned the case of reassigning the member when it is non-nil. &nbsp;You would have to require users to verify it is nil before setting it or if it is not nil, calling it before assigning to it.<br><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div>And if you have a model that relies on behavior in a deinit then storing the closure won't be possible for structs.</div></div></blockquote><div>&nbsp;</div><div>This is true, considering you don't want to copy a @once closure you probably don't want value-type semantics anyway.</div></div></div></div></div></blockquote><div><br></div><div>That's a good point. &nbsp;Since we don't have control over copy behavior in Swift it wouldn't make sense at all unless / until we can make structs that have move semantics (maybe if / when we get a Rust-like ownership system?).</div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div>You have also missed the case that the closure is captured by another closure (maybe it is a completion block and you call it in a completion block of a method your method calls).</div></div></blockquote><div>&nbsp;</div><div>This is correct. I forgot to mention that I'm sorry, thanks for pointing it out!</div><div><br></div><div>I was thinking that a @once closure can only be captured by another @once closure. We can add that as another dot-point:</div><div><br></div><div><div><ul><li>the closure is captured by another @once closure, this is the only time it can be captured.<br></li></ul></div></div></div></div></div></div></blockquote><div>I like the idea behind this proposal in theory. &nbsp;However, it really seems to cry out for linear types. &nbsp;I have a feeling we would end up with a better (and more general) solution if Swift goes down that path in the future. &nbsp;At minimum, I would like to hear input from those who work on the type system. &nbsp;If a more robust, less ad-hoc solution will be possible in the future it might be best to wait.</div><div><br></div><div>On the other hand, completion callbacks that require this guarantee are pretty common. &nbsp;The semantic is part of the contract whether we have language support for it or not. &nbsp;Maybe we can do something now that could be subsumed by a more general feature in the future...</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>&nbsp;</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><div class="h5"><div><br></div><blockquote type="cite"><div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 5, 2016 at 11:59 PM, Matthew Johnson <span dir="ltr">&lt;<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br><br>Sent from my iPad</div><span><div><br>On Jun 5, 2016, at 8:52 AM, Andrew Bennett &lt;<a href="mailto:cacoyi@gmail.com" target="_blank">cacoyi@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Storing into a member would be fine, as long as it must keep&nbsp;<span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">@once</span>&nbsp;as a type annotation and the compiler makes sure you maintain:<div><span style="font-family:Menlo;font-size:11px">&nbsp; &nbsp; sum</span><font face="monospace, monospace">(callCount, storeCount, passCount) == 1</font><div><br></div><div>For example:</div><div>&nbsp;&nbsp;<span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">class&nbsp;</span><span style="font-family:Menlo;font-size:11px">Example {</span><br><div><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">&nbsp; &nbsp;&nbsp;</span><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">private </span><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">var&nbsp;</span><span style="font-family:Menlo;font-size:11px">closure: (</span><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">@once</span><span style="font-family:Menlo;font-size:11px">&nbsp;(</span><span style="font-family:Menlo;font-size:11px">T) -&gt;&nbsp;</span><span style="font-family:Menlo;font-size:11px">Void)?</span></div><div><span style="font-family:Menlo;font-size:11px"><br></span></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp;&nbsp;</span><span style="color:rgb(187,44,162)">func</span><span> callClosure(value: </span><span style="color:rgb(112,61,170)">T,&nbsp;</span>replace<span style="color:rgb(112,61,170)">:&nbsp;</span>(<span style="color:rgb(187,44,162)">@once</span>&nbsp;(T) -&gt;&nbsp;Void)? =&nbsp;<span style="color:rgb(187,44,162)">nil</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color:rgb(0,132,0)">// the compiler should error if it detects the closure:</span></p><p style="margin:0px;line-height:normal"><font color="#008400" style="font-family:Menlo;font-size:11px">&nbsp; &nbsp; &nbsp; // &nbsp;* escaping more than once, while still being&nbsp;</font><font color="#008400" face="Menlo"><span style="font-size:11px">stored,</span></font></p><p style="margin:0px;line-height:normal"><font color="#008400" face="Menlo"><span style="font-size:11px">&nbsp; &nbsp; &nbsp; //&nbsp;</span></font><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">&nbsp;*</span><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">&nbsp;</span><span style="font-size:11px;color:rgb(0,132,0);font-family:Menlo">or being called while still being stored or escaping,</span></p><p style="margin:0px;line-height:normal"><font color="#008400" face="Menlo"><span style="font-size:11px">&nbsp; &nbsp; &nbsp; //&nbsp;</span></font><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">&nbsp;*</span><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">&nbsp;</span><span style="font-size:11px;color:rgb(0,132,0);font-family:Menlo">or being overwritten without being called</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">if</span><span> </span><span style="color:rgb(187,44,162)">let</span><span> closure = </span><span style="color:rgb(187,44,162)">self</span><span>.closure {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">self</span><span>.closure =&nbsp;</span>replace</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; &nbsp; closure(value)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">deinit</span><span> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color:rgb(0,132,0)">// compiler warning: that closure is potentially un-called</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(0,132,0)">&nbsp; &nbsp; &nbsp; // runtime fatalError if it's .Some(Closure) after deinit</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; }</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; }</span></p></div><div><br></div><div>There could be a standard library type with those guarantees built in.</div></div></div></div></div></blockquote><div><br></div></span><div>I don't consider this compiler verification.&nbsp; It is runtime verification.&nbsp; The best the compiler can do is enforce constraints that allow for guaranteed runtime verification.&nbsp; You can argue that is better than nothing but it is not a static guarantee of correct behavior.</div><div><div><br><blockquote type="cite"><div><div dir="ltr"><div><div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 5, 2016 at 10:12 PM, Matthew Johnson <span dir="ltr">&lt;<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br><br>Sent from my iPad</div><span><div><br>On Jun 5, 2016, at 6:56 AM, Andrew Bennett &lt;<a href="mailto:cacoyi@gmail.com" target="_blank">cacoyi@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">I like this.<div><br></div><div>One of the suggestions on @noescape(once) was that it just becomes @once and works with escaping closures too. It might be possible if compile time checks verified that the closure isn't copied, and that it is called before being deinit-ialized. Failing that I'm happy with a runtime circumstance in the cases the compiler can't check.</div></div></div></blockquote><div><br></div></span>Yeah, maybe if it is only used asynchronously and never stored in a member or global it could be verified and that is a pretty common case.&nbsp; That would certainly be easier than the general case.<div><br></div><div><div>I prefer @once over @required if the guarantee is single execution.&nbsp; If the guarantee is *at least once* obviously @once is not the right attribute, but I'm not convinced @required is either.&nbsp; Maybe @invoked.</div><div><div><div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>It would be great if @required took into the account the feedback from that proposal and considered the synchronous case too.</div><div><br></div><div>As an aside, you can get some of the guarantees you want like this:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span><span> doSomething(completionHandler: (</span><span style="color:rgb(79,129,135)">SomeEnum</span><span>) -&gt; ()) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)">&nbsp; </span><span>dispatch_async</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(79,129,135)">someQueue</span><span style="color:rgb(0,0,0)">) {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">let</span><span> result: </span><span style="color:rgb(79,129,135)">SomeEnum</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(0,0,0)">&nbsp; &nbsp; </span><span style="color:rgb(0,132,0)">// the compiler ensures 'result' is set</span><span style="color:rgb(79,129,135)"><br></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">defer</span><span> { completionHandler(result) }</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">if</span><span style="color:rgb(0,0,0)"> </span><span>aCondition</span><span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">&nbsp; &nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">if</span><span style="color:rgb(0,0,0)"> </span><span>bCondition</span><span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; &nbsp; result = .</span><span style="color:rgb(49,89,93)">Foo</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; } </span><span style="color:rgb(187,44,162)">else</span><span> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; &nbsp; result = .</span><span style="color:rgb(49,89,93)">Bar</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; }</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">&nbsp; &nbsp; &nbsp;&nbsp;</span><span>// the compiler ensures you do this, because it is 'let'</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">return</span></p><div><span style="font-family:Menlo;font-size:11px">&nbsp; &nbsp;&nbsp;</span><span style="font-family:Menlo;font-size:11px">}</span><br></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)">if</span><span style="color:rgb(0,0,0)"> </span><span>cCondition</span><span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; &nbsp; result = .</span><span style="color:rgb(49,89,93)">Baz</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; &nbsp; }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>&nbsp; }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></p><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 5, 2016 at 9:42 PM, Matthew Johnson 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br><br>Sent from my iPad</div><span><div><br>On Jun 5, 2016, at 5:02 AM, Patrick Pijnappel via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">This has actually been proposed before, see SE-0073:&nbsp;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0073-noescape-once.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0073-noescape-once.md</a></div></div></blockquote><div><br></div></span>Actually that proposal was for noescape closures and this suggestion is for escaping closures.&nbsp; I don't think the compiler can verify this for noescape closures.&nbsp; If it is possible it would be far more complicated.<div><div><div><br><blockquote type="cite"><div><div dir="ltr"><div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 5, 2016 at 11:37 AM, Charles Srstka 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">MOTIVATION:<br>
<br>
As per the current situation, there is a pitfall when writing asynchronous APIs that does not occur when writing synchronous APIs. Consider the following synchronous API:<br>
<br>
func doSomething() -&gt; SomeEnum {<br>
&nbsp; &nbsp; &nbsp; &nbsp; if aCondition {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bCondition {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return .Foo<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return .Bar<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
&nbsp; &nbsp; &nbsp; &nbsp; } else {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if cCondition {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return .Baz<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
&nbsp; &nbsp; &nbsp; &nbsp; }<br>
}<br>
<br>
The compiler will give an error here, since if both aCondition and cCondition are false, the function will not return anything.<br>
<br>
However, consider the equivalent async API:<br>
<br>
func doSomething(completionHandler: (SomeEnum) -&gt; ()) {<br>
&nbsp; &nbsp; &nbsp; &nbsp; dispatch_async(someQueue) {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if aCondition {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bCondition {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; completionHandler(.Foo)<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; completionHandler(.Bar)<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if cCondition {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; completionHandler(.Baz)<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>
&nbsp; &nbsp; &nbsp; &nbsp; }<br>
}<br>
<br>
Whoops, now the function can return without ever firing its completion handler, and the problem might not be discovered until runtime (and, depending on the complexity of the function, may be hard to find).<br>
<br>
PROPOSED SOLUTION:<br>
<br>
Add a @required attribute that can be applied to closure arguments. This attribute simply states that the given closure will always be eventually called, and the compiler can enforce this.<br>
<br>
DETAILED DESIGN:<br>
<br>
- The @required attribute states in our API contract that a given closure *must* be called at some point after the function is called.<br>
<br>
- Standard API calls like dispatch_async that contractually promise to execute a closure or block get @required added to their signatures.<br>
<br>
- When the compiler sees a @required closure in a function declaration, it checks to make sure that every execution path either calls the closure at some point, or sends a @required closure to another API that eventually ends up calling the closure.<br>
<br>
- If there’s a way for a @required closure not to be called, the compiler emits an error letting the developer know about the bug in his/her code.<br>
<br>
IMPACT ON EXISTING CODE:<br>
<br>
None. This is purely additive.<br>
<br>
ALTERNATIVES CONSIDERED:<br>
<br>
I got nothin’.<br>
<br>
Charles<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div><br></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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></div>
</div></blockquote></div></div></div></div></div></blockquote></div><br></div>
</div></blockquote></div></div></div></blockquote></div><br></div>
</div></blockquote></div></div></div></blockquote></div><br></div></div>
</div></blockquote></div></div></body></html>