<div dir="ltr">I suggested exactly the same thing a few days ago as [firm self]. I like [guard self] even better.<div>+1</div><div><br></div><div>Kurt</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 3, 2016 at 9:57 PM, Evan Maloney via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">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><br></div><div>I propose:<div><br></div><div>let doSomething: () -> Void = { [<b>guard</b> self] in</div><div> ...</div><div>}</div><div><div><br></div><div>[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><br></div><div>So, when doSomething() is just sitting around <i>not</i> doing something, it doesn't prevent self from being deallocated.</div><div><br></div><div>If, by the time doSomething() is called, self is no longer there, doSomething() just returns without the closure executing. If self <i>is</i> there, then the closure executes with self as a strong reference inside. </div><div><br></div><div><div>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><br></div><div>Ok, so what closures with a return value, you ask? How about something like:</div><div><br></div><div><div>let maybeDoSomething: () -> Bool = { [guard self else false] in</div><div> ...</div><div>}</div><div><br></div><div>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><br></div><div>What do you think?</div><div><br></div><div></div><div><br><div><blockquote type="cite"><div>On Jan 28, 2016, at 7:32 PM, Hoon H. via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div>Thanks for your opinions.</div><div>I am writing a formal proposal, and now I think it’d be fine to elide explicit `self` qualification after `guard let … `.</div><div><br></div><div>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><div>
<div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>— Hoon H.</div><div><br></div></div><br></div><br><br>
</div>
<br><div><blockquote type="cite"><div>On 2016/01/06, at 10:46 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>> wrote:</div><br><div><div dir="ltr">+1.<div><br></div><div>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><br></div><div>In many cases, I use the pattern</div><div><br></div><div> somethingAsync { [weak self] in</div><div> guard let strongSelf = self else { return }</div><div><br></div><div> // use strongSelf below</div><div> }</div><div><br></div><div>But of course, this leads to the unnatural/unwieldy "strongSelf.property" all over the place.</div><div><br></div><div>I agree with Jordan that "guard let self = self" isn't the most satisfying syntax, but it has the advantage of being a <i>very</i> minimal grammar/syntax change, and its behavior is completely clear as long as the user is already familiar with guard.</div><div><br></div><div>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"><div><div><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Tue, Jan 5, 2016 at 4:20 PM, Jordan Rose via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></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 style="word-wrap:break-word"><div>This has come up before, in a thread called "<span style="font-family:'Helvetica Neue'">Proposal: weakStrong self in completion</span> <span style="font-family:'Helvetica Neue'">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><br></div><div>Jordan</div><div><div><div><br></div><br><div><blockquote type="cite"><div>On Jan 5, 2016, at 16:02, Hoon H. via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div>Currently, weakly captured `self` cannot be bound to `guard let …` with same name, and emits a compiler error.<br><br><span style="white-space:pre-wrap">        </span>class Foo {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>func test2(f: ()->()) {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>// … <br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>func test1() {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>test2 { [weak self] in<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>guard let self = self else { return } // Error.<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>print(self)<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span>}<br><br>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><br><span style="white-space:pre-wrap">        </span>class Foo {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>func getValue1() -> Int {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>return 1234<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>func test3(value: Int) {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>print(value)<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>func test2(f: ()->()) {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>// … <br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>func test1() {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>test2 { [weak self] in<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>self?.test3(self?.getValue1()) // Doesn't work because it's not unwrapped.<br><br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>self!.test3(self!.getValue1()) // Considered harmful due to `!`.<br><br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>guard self != nil else { return }<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>self!.test3(self!.getValue1()) // OK, but still looks and feels harmful.<br><br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>guard let self1 = self else { return }<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>self1.test3(self1.getValue1()) // OK, but feels ugly due to unnecessary new name `self1`.<br><br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>guard let self = self else { return }<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>self.test3(self.getValue1()) // OK.<br><br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span>}<br><br>This also can be applied to `if let` or same sort of constructs.<br><br>Even further, we can consider removing required reference to `self` after `guard let …` if appropriate.<br><br><span style="white-space:pre-wrap">        </span>guard let self = self else { return } <br><span style="white-space:pre-wrap">        </span>test3(getValue1()) // Referencing to `self` would not be required anymore. Seems arguable.<br><br>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><br>I am not sure whether this is already supported or planned. But lacked at least in Swift 2.1.1.<br><br>— Hoon H.<br><br><br><br><br><br><br><br><br><br><br><br><br><br><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" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></div></blockquote></div><br>
</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">
</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></blockquote></div><br></div>_______________________________________________<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" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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><br clear="all"><div><br></div>-- <br><div class="gmail_signature">kurt@CircleW.org<br><a href="http://www.CircleW.org/kurt/" target="_blank">http://www.CircleW.org/kurt/</a><br></div>
</div>