[swift-evolution] Allowing `guard let self = self else { ... }` for weakly captured self in a closure.

Chris Lattner clattner at apple.com
Fri Jan 22 20:08:30 CST 2016


> On Jan 22, 2016, at 5:54 PM, Jordan Rose <jordan_rose at apple.com> wrote:
> 
> 
>> On Jan 22, 2016, at 17:51, Chris Lattner <clattner at apple.com> wrote:
>> 
>> 
>>> On Jan 5, 2016, at 8:21 PM, Christopher Rogers via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> You can shadow self with a guard like you wrote it if use the keyword escaping backquotes like so:
>>> 
>>> guard let `self` = self else { return }
>> 
>> To confirm what others have said down thread, this is a compiler bug: this code is valid, but shouldn’t change what references to “self.method()” or “method()" bind to (i.e., explicit or implicit references to self).
>> 
>> Backquotes are for forming a name that happens to overlap with a keyword.  In the case of `self`, this could be because you want to refer to the NSObject.`self` method for some reason.  Backquotes are not a way to name self, init, subscript or any of the other declarations that look like magic identifiers.
> 
> …which we ought to fix by not reusing Identifier data for the keywords. ;-)

The fix is to give DeclName more teeth, which I’m considering working on.  The details of the implementation aspects don’t seem important for swift-evolution though.

-Chris


More information about the swift-evolution mailing list