<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="">I'm way-late to this discussion—I don't know how any of you get any coding done trying to keep up with this mailing list!—but anyway...<div class=""><br class=""></div><div class="">I propose:<div class=""><br class=""></div><div class="">let doSomething: () -> Void = { [<b class="">guard</b> self] in</div><div class=""> ...</div><div class="">}</div><div class=""><div class=""><br class=""></div><div class="">[guard self] would effectively work like [weak self] in that it doesn't cause the closure itself to hold a strong reference to whatever is pointed to by self. But if self is still around when the closure is called, it upgrades it to a strong reference for the duration of the closure's execution.</div><div class=""><br class=""></div><div class="">So, when doSomething() is just sitting around <i class="">not</i> doing something, it doesn't prevent self from being deallocated.</div><div class=""><br class=""></div><div class="">If, by the time doSomething() is called, self is no longer there, doSomething() just returns without the closure executing. If self <i class="">is</i> there, then the closure executes with self as a strong reference inside. </div><div class=""><br class=""></div><div class=""><div class="">That way, self within the closure is already strong, so you can use it conveniently as a non-optional and without doing the strongSelf = self dance. You get the memory management benefit of a weak reference without the extra noise in your code needed to support it.</div></div><div class=""><br class=""></div><div class="">Ok, so what closures with a return value, you ask? How about something like:</div><div class=""><br class=""></div><div class=""><div class="">let maybeDoSomething: () -> Bool = { [guard self else false] in</div><div class=""> ...</div><div class="">}</div><div class=""><br class=""></div><div class="">Here, maybeDoSomething() doesn't hold a strong reference to self. If it executes when self is still alive, the code within the braces executes with self as a strong reference. If self is gone, the value after the else is returned (the "return" itself is implied).</div><div class=""><br class=""></div><div class="">What do you think?</div><div class=""><br class=""></div><div class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 28, 2016, at 7:32 PM, Hoon H. via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thanks for your opinions.</div><div class="">I am writing a formal proposal, and now I think it’d be fine to elide explicit `self` qualification after `guard let … `.</div><div class=""><br class=""></div><div class="">Also for your proposal, though I think mine is originated from different intention, but final conclusion overlaps with your intention, and I am still not sure what to do in this situation. Do you have some opinions?</div><br class=""><div class="">
<div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">— Hoon H.</div><div class=""><br class=""></div></div><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"><br class="Apple-interchange-newline">
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On 2016/01/06, at 10:46 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">+1.<div class=""><br class=""></div><div class="">Merely using "self?.something" repeatedly might produce unexpected behavior, if self becomes nil between calls. As I mentioned in another thread, in Obj-C, there is a warning for this (-Warc-repeated-use-of-weak).</div><div class=""><br class=""></div><div class="">In many cases, I use the pattern</div><div class=""><br class=""></div><div class=""> somethingAsync { [weak self] in</div><div class=""> guard let strongSelf = self else { return }</div><div class=""><br class=""></div><div class=""> // use strongSelf below</div><div class=""> }</div><div class=""><br class=""></div><div class="">But of course, this leads to the unnatural/unwieldy "strongSelf.property" all over the place.</div><div class=""><br class=""></div><div class="">I agree with Jordan that "guard let self = self" isn't the most satisfying syntax, but it has the advantage of being a <i class="">very</i> minimal grammar/syntax change, and its behavior is completely clear as long as the user is already familiar with guard.</div><div class=""><br class=""></div><div class="">We should also consider whether "self." is required after "guard let self = self". An explicit "guard let self = self" avoids the accidental-capture problem, so I think it's reasonable to allow unqualified property access for the remainder of the scope.</div><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 Tue, Jan 5, 2016 at 4:20 PM, Jordan Rose via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><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 style="word-wrap:break-word" class=""><div class="">This has come up before, in a thread called "<span style="font-family:'Helvetica Neue'" class="">Proposal: weakStrong self in completion</span> <span style="font-family:'Helvetica Neue'" class="">handler closures". I'm still not 100% happy with the syntax, but I like that "guard let" can handle non-Void non-Optional returns well, while 'weakStrong' cannot.</span></div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><div class="h5"><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 5, 2016, at 16:02, Hoon H. via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""><div class=""><div class="">Currently, weakly captured `self` cannot be bound to `guard let …` with same name, and emits a compiler error.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span>class Foo {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>func test2(f: ()->()) {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>// … <br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>func test1() {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>test2 { [weak self] in<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>guard let self = self else { return } // Error.<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>print(self)<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span>}<br class=""><br class="">Do we have any reason to disallow making `self` back to strong reference? It’d be nice if I can do it. Please consider this case.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span>class Foo {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>func getValue1() -> Int {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>return 1234<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>func test3(value: Int) {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>print(value)<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>func test2(f: ()->()) {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>// … <br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>func test1() {<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>test2 { [weak self] in<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>self?.test3(self?.getValue1()) // Doesn't work because it's not unwrapped.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>self!.test3(self!.getValue1()) // Considered harmful due to `!`.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>guard self != nil else { return }<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>self!.test3(self!.getValue1()) // OK, but still looks and feels harmful.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>guard let self1 = self else { return }<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>self1.test3(self1.getValue1()) // OK, but feels ugly due to unnecessary new name `self1`.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>guard let self = self else { return }<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>self.test3(self.getValue1()) // OK.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span><span style="white-space:pre-wrap" class="">        </span>}<br class=""><span style="white-space:pre-wrap" class="">        </span>}<br class=""><br class="">This also can be applied to `if let` or same sort of constructs.<br class=""><br class="">Even further, we can consider removing required reference to `self` after `guard let …` if appropriate.<br class=""><br class=""><span style="white-space:pre-wrap" class="">        </span>guard let self = self else { return } <br class=""><span style="white-space:pre-wrap" class="">        </span>test3(getValue1()) // Referencing to `self` would not be required anymore. Seems arguable.<br class=""><br class="">I think this is almost fine because users have to express their intention explicitly with `guard` statement. If someone erases the `guard` later, compiler will require explicit self again, and that will prevent mistakes. But still, I am not sure this removal would be perfectly fine.<br class=""><br class="">I am not sure whether this is already supported or planned. But lacked at least in Swift 2.1.1.<br class=""><br class="">— Hoon H.<br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class="">
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qyMAkg2Lbcvsx-2FLNqi3oOdJt1QAbQshlb3be28ssdS5kpemhfk4iS37uFqs1vYLOk-2FGnqdGh6EnQsqlDZisN6lPIU-2BNnZD1eKXlc9oVM4EZMzk3cC9vnt8QPG9G4McholxrqxJrJSBO9LVBY6xWow-2FU1o-2BdTp4Fzjqn468cHUjlsfCtZMkoMR25GlMgj518QVQ-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class="">
</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>
</div></blockquote></div><br class=""></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=""></div></div></div></div></body></html>