<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">On Jun 26, 2016, at 4:25 PM, Russ Bishop &lt;<a href="mailto:xenadu@gmail.com" class="">xenadu@gmail.com</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jun 26, 2016, at 12:10 PM, Christopher Kornher via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I may be too late for Swift 3, but I am planning to propose changes to the default behavior for closures capturing object references. The introduction of Swift Playgrounds&nbsp;has raised the importance of simplifying the coding of leak-free, crash-free closures. New developers should not have to understand closure memory management to&nbsp;start writing useful and correct code.<br class=""><br class="">The topic of the closure weak/strong dance has been discussed on this list before. This proposal differs from previous proposals in that it will eliminate the dance&nbsp;altogether by default. I am very interested in hearing others’ opinions as to whether the benefits outweigh the costs of various options.<br class=""></blockquote><br class="">The problem is that strong reference capture is probably the far more common case.<br class=""></blockquote><br class="">Strong reference capture has not been more common in carefully written code in my experience. Swift is starting to be used for many different problem&nbsp;domains, so your experience may be different. Any examples of real-world code would be greatly appreciated.<br class=""><br class="">Sometimes closures contain the only references to objects, but this is not common in code that I have seen. It would be extremely rare for strong references to&nbsp;be required for all the references in closure with multiple references (the current default behavior). Long closures can reference many objects and it is all too&nbsp;easy to leave a reference out of the capture list and create a reference cycle.&nbsp;I believe that this pattern should be called-out (see below).<br class=""><br class="">Strong references are occasionally needed to ensure operations are performed when objects would otherwise be reclaimed, but I have not seen many of&nbsp;these cases. This pattern could be more common in other frameworks. I believe that this pattern should be called-out (see below).<br class=""><br class="">Multiple capture rules for closures can be supported if desired. The ```[required…``` capture qualifier in the original email is one way todo this. The question&nbsp;mark could be used in a way analogous to `try?` to identify closures using the proposed rules:&nbsp;<br class=""><br class="">```let a:()-&gt;Void = {…}?```<br class="">or&nbsp;<br class="">``` let a:()-&gt;Void = ?{…}```<br class="">etc.<br class=""><br class="">This would obviously add more complexity but would still be an improvement, I believe.<br class=""><br class=""><br class=""><blockquote type="cite" class="">If you wanted to say that @noescape closures capture references strongly by default, while escaping closures capture them weakly by default that may be&nbsp;closer to reasonable for most situations but now you have magic behavior with an even greater cognitive overhead. (I wonder if it would be more common&nbsp;that @noescape captures strongly, escaping captures self weak by default but other objects strongly… of course that would have even worse cognitive&nbsp;overhead.)<br class=""></blockquote><br class="">I did consider treating self differently, but this leads to some very strange cases when delegation, factories and other patterns are considered.&nbsp;<br class=""><br class="">This email was implicitly referring to escaping uses of closures. The same closure can be used as escaping or non-escaping. The Swift documentation states:<div class=""><br class=""></div><div class="">&nbsp;"<i class="">Marking a closure with&nbsp;@noescape&nbsp;lets the compiler make more aggressive optimizations because it knows more information about the&nbsp;closure’s lifespan.</i>"<br class=""><br class="">@noescape is essentially a hint to the compiler. Optimizers would be free to use strong or unowned references if they can determine&nbsp;that it is safe to do so without altering behavior.&nbsp;<br class=""><br class=""><blockquote type="cite" class="">No matter what, without a garbage collector you are stuck with sub-optimal solutions for fixing reference cycles. I could imagine a language feature that&nbsp;automatically detected trivial cycles (A -&gt; B -&gt; A) but anything more complex just becomes a form of garbage collection anyway.<br class=""></blockquote><br class="">Object networks are difficult enough with ARC without dozens of closures with unnecessary strongly captured references. The new Xcode tools will be a huge&nbsp;help with leaks, but they should not be required.<br class=""><br class=""><blockquote type="cite" class=""><br class="">I don’t think there is a way to square this circle. Either you have one “automagic” behavior that is wrong for some cases (whether strong or weak is the&nbsp;default), or you require everyone to spam their closures with explicit capture annotations even if there’s a default “capture everything strongly” variant (see&nbsp;C++ lambdas).<br class=""></blockquote><br class="">Yes, we are discussing tradeoffs. Writing correct code with closures will always require care.I believe that is is better to have safe, leak free code by default&nbsp;than not. I also believe that capturing strong references by default can probably lead to at least as many unexpected behaviors as capturing weak references. I&nbsp;don’t think that many developers would expect, for example, to see zombie view controllers that have not been associated with an active view hierarchy for&nbsp;weeks because a closure is holding on to a strong reference.<br class=""><br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class=""><br class="">Use of ‘unowned’&nbsp;<br class="">————————<br class=""><br class="">I now routinely create closures that capture `self` and other object references as ‘weak’ even if I think that I feel that `unowned ` would be safe. This may not be the&nbsp;absolutely most performant solution, but it is straightforward and robust.<br class=""></blockquote><br class="">I agree and our team has adopted the rule that use of unowned is not allowed unless the declaration is private and there is profiler proof that it represents a&nbsp;performance problem, and if used warning comments must be placed in the code. Weak is almost never a performance problem and eliminates the risk of a&nbsp;crash, so it is highly preferable to unowned.<br class=""><br class="">I’d go so far as to say unowned should be removed; let the user use Unmanaged&lt;T&gt; if they need to capture an unowned reference. They’re entering expert&nbsp;territory anyway.<br class=""></blockquote><br class="">I use ‘unowned' regularly for back pointers but not closures. The documentation should contain more warnings about the use of `unowned` at least.<br class=""><br class=""><blockquote type="cite" class=""><br class=""><br class=""><br class=""><blockquote type="cite" class=""><br class="">The core proposal:<br class="">——————<br class=""><br class="">Closures capturing object references should automatically capture all object references as weak.<br class=""><br class=""></blockquote><br class="">This becomes a form of the Objective-C messages-to-nil-do-nothing problem where you don’t crash but your closure doesn’t do any work (or does half the&nbsp;work!) because the reference(s) are/become nil. It doesn’t save you from reasoning about object lifetime because it is just as easy for the closure to capture&nbsp;the last reference to an object or for the lifetime to differ from the closure lifetime. You’re just trading reference cycles for a different problem.<br class=""></blockquote><div class=""><br class=""></div><div class="">Yes, this is based upon the opinion that operations on objects that would otherwise have been reclaimed very often can be ignored. If they cannot be ignored, the reference must be captured strongly, but this is relatively rare and should probably be called-out anyway, given the way that developers are forced to automatically invoke the weak/strong dance when creating closures. This suggestion is based upon my experience with thousands(?) of Swift closures in iOS apps in the past two years.</div><blockquote type="cite" class=""><blockquote type="cite" class=""><br class=""><br class="">2) Some of the magic in #1 could be eliminated by introducing a new capture type: &nbsp;‘required’ to specify ‘weak guarded’ captures, allowing the example closure to be&nbsp;written:<br class=""><br class=""></blockquote><br class="">This has been debated before. I support the idea of a “required” capture specifier but IIRC the core team was not supportive of the idea because it adds&nbsp;another layer of magic on the existing magic (object deallocated magically means your closure never executes).<br class=""></blockquote><div class=""><br class=""></div><div class="">The increase in complexity may not be worth the elimination of guard statements. This proposal is different in that it retains a single capture default for closures. The ‘required’ keyword does not change the way references are captured. This may be a significant difference or not.</div><br class=""><blockquote type="cite" class=""><br class="">Russ<br class=""><br class=""></blockquote><br class=""></div></body></html>