<div dir="ltr">Wow! I didn't know that worked. It's a bit surprising, and perhaps not intended. I think the proposal is still valid.<div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 5, 2016 at 8:21 PM, Christopher Rogers <span dir="ltr"><<a href="mailto:christorogers@gmail.com" target="_blank">christorogers@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You can shadow self with a guard like you wrote it if use the keyword escaping backquotes like so:<br><br>guard let `self` = self else { return }<br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 6, 2016 at 10:55 AM Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">FWIW, in a codebase of ~150 Swift files, I see 18 occurrences of "let strongSelf = self", and 26 occurrences of "self?." (which should arguably be changed to the former).<div class="gmail_extra"></div></div><div dir="ltr"><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div>Jacob<br></div></div></div></div></div></div><div dir="ltr"><div class="gmail_extra">
<br><div class="gmail_quote">On Tue, Jan 5, 2016 at 5:46 PM, Jacob Bandes-Storch <span dir="ltr"><<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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"><span><font color="#888888"><br clear="all"><div><div><div dir="ltr"><div>Jacob<br></div></div></div></div></font></span><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></div>
</blockquote></div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=W77bTy6YRdHySgTK0Dy8RU-2F4RANwbXKARV3F91EwXPp38R4thIsvXKvNNaVHobAbhgVDdoda9H7-2F2yQL9Z1xipj2-2BN111n8fZKCk5fl33UaNes5WfsfnDqiYEkCvAqi0o1CY0HQLDy81GJTgG6e6n6QfrmqtUtbIlL4MhPRCGpuocIP-2Bos1IMc6s0KMGJAnemrPExvYowVK19LFjfvHgspXBy-2FUxDTBZg43gXpYctiA-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<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>
</blockquote></div><br></div></div>