[swift-evolution] Proposal: Re-instate mandatory self for accessing instance properties and functions

Charles Srstka cocoadev at charlessoft.com
Sun Dec 13 15:45:24 CST 2015


> On Dec 13, 2015, at 1:15 AM, Rob Napier via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I wanted to reopen this discussion that seems to have trailed off. Requesting the return of self was my very first ask of Swift if I remember correctly (https://devforums.apple.com/message/1013085 <https://devforums.apple.com/message/1013085>). Continued work in Swift has both strengthened and modified that ask. Here are several of the examples discussed before:
> 
> https://gist.github.com/schwa/94b11dc0a7a331f46b25 <https://gist.github.com/schwa/94b11dc0a7a331f46b25>
> https://gist.github.com/rnapier/478465d1b15e95b98b42 <https://gist.github.com/rnapier/478465d1b15e95b98b42>
> https://gist.github.com/rnapier/4213dc64206b17df6935 <https://gist.github.com/rnapier/4213dc64206b17df6935>
> https://gist.github.com/dwineman/d6c56ec0c0e2fdb761db <https://gist.github.com/dwineman/d6c56ec0c0e2fdb761db>
> 
> I get that it seems tedious to type (and read) "self." and I get that "self." is currently a hint that self might be captured (but doesn't actually mean that, since you can use self. without capturing, and sometimes have to, very often in init, so really it's basically meaningless for that use).
> 
> That's why I suggest using .x and .f() to mark implicit self. I realize that this may conflict with enum usage. If so, then use another marker. For instance :x or ^x or anything. But implicit self is confusing in a lot of code, can (and does) lead to shadowing bugs, and is very difficult to reason about in diffs or any other interface that isn't an IDE (especially code review).
> 
> Thoughts, David? I agree with your basic proposal; I just want to amend it.
> 
> -Rob
> 
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

Since opinions seem to be divided on this, how about adding an optional warning flag to the compiler, which would warn if one accessed an instance variable or method without self. in front? That way, each person could choose whether they wanted implicit ‘self.’ or not. I know that this warning would be helpful to me, as I’ve already run into some rather weird bugs due to the implicit self, and it would be one less thing to debug.

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151213/71162521/attachment.html>


More information about the swift-evolution mailing list