[swift-evolution] [Review] Require self for accessing instance members

Denis Nikitenko d.nikitenko at icloud.com
Wed Dec 16 22:40:38 CST 2015

* What is your evaluation of the proposal?

I vote to reject the proposal.

* Is the problem being addressed significant enough to warrant a change to Swift?

No.  It tries to address potential errors caused by variable shadowing.  However, I feel that this comes at the cost of making code less readable and overly verbose, and potentially hiding memory leaks caused by strong references in closures.  I do not believe this trade-off is worthwhile.  I echo the opinion of other participants who feel that this is something that should be enforced by in-house standards, rather than by the language itself.

From a pedagogical perspective, I found that students rarely encounter errors caused by confusion between instance variables and local variables.  However memory management in general makes their heads explode.  Anything that attracts attention to potential memory issues is definitely a plus in my book.   As a computer science instructor, I like always having self in closures, but not requiring it elsewhere.

* Does this proposal fit well with the feel and direction of Swift?

No, I don’t believe so.  The current usage strikes a good balance between clarity and safety.  I do not think that explicit self adds to either clarity or safety.

* If you have you used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

I have used C++, Java, Swift, and C#, all of which have “this/self” keyword for explicit access of instance members, but do not require its use.  These languages use it sparingly, typically when initializing instance variables in constructors and init/set methods.  I’ve also noticed partial classes in C# sometimes using “this" to emphasize that a particular instance member was defined in a separate source file.

A single opinion is just one data point and should be treated as such, but in my programming experience I did not encounter many shadowing errors that would have been prevented by an explicit use of “this/self”.  If we use it everywhere, we lose the ability to use it for signalling something really important.

* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

I have been following this discussion since the beginning and read the arguments from both sides.  I‘ve spent some time over the last few days reflecting on my past experiences and I have reviewed some of the Swift, C#, and C++ code on my hard drive to see where self/this is used (both in my code and in source code of 3rd party libraries).



More information about the swift-evolution mailing list