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

Benjamin Deming ben at bendeming.com
Sun Dec 20 20:32:56 CST 2015


* What is your evaluation of the proposal?

I am against.

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

No. 

Quoting the proposal: 

> Lets the compiler warn users (and avoids bugs) where the authors mean to use a local variable but instead are unknowingly using an instance property (and the other way round).

I’m unsure that this is so prevalent a programmer error that it should dictate an eventual breaking change in the language (in the proposal, the breaking change would occur starting in Swift 3.0). The provided example shows commented out declaration of a local variable that shadows an instance variable. That is the only apparent situation one would incur mistaken use of an instance member instead of a local variable having the same name. The proposed change seems like a large undertaking to prevent such a situation. Because whenever such a line would *not* be commented out, you would end up using the local variable because it shadows the instance variable. 

The accidental commenting out of a local variable declaration would be easier to spot during a review or bug fixing than the “other way round”: meaning to use an instance member, but instead using a local variable. But one can enable the -Wshadow clang flag, and the compiler will omit a warning when shadowing of an instance member by a local variable occurs. That would fulfill the proposal’s desire for a compiler warning––for this case––although it would not be on by default. But for projects concerned by shadowing of instance members, it could be an option.

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

My take on this is more handwave-y and philosophical as a lot of stylistic opinions can be. Requiring use of self when accessing instance members would feel like a vestige from Objective-C because you must do it in Objective-C and not in Swift.

For many, the majority of the time (e.g., when not specifying custom setters or getters for properties), use of properties *feel* much more like direct manipulation of backing storage than in Objective-C because you don’t have to specify `self` as when writing in Objective-C. In Obj-C, when setting or getting some property, you are sending a message to self, so the required syntax, while still boilerplate-y, looks just like invocation of other kinds of methods either using brackets or dot syntax. Default setter and getter synthesis is less apparent in Swift..

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

Equivalent of a glance. I’ve been writing primarily in Swift for the last few months and being able to omit self has been really nice. It’s one of the places where Swift’s terseness feels most comfortable for me.

> On Dec 20, 2015, at 7:54 AM, Kevin Lundberg via swift-evolution <swift-evolution at swift.org> wrote:
> 
> -1, for this reason, and for the extreme loss in conciseness already mentioned elsewhere. The purported extra clarity is not worth the extra burden placed on writing code in my opinion. I would favor a compiler warning at the most, and this can be solved on an individual/team basis with linting tools as well.
> 
> On 12/18/2015 1:02 AM, Jed Lewison via swift-evolution wrote:
>> I’m not in favor of this proposal, and rather than repeat arguments that have already been made, I thought I’d share a small piece of data from the project I’m working on to illustrate the impact of implicit self in terms of reducing repetitive boilerplate cruft.
>> 
>> Our project consists of a legacy ObjC code base for an iOS app and a new version written entirely in Swift. The feature set is largely the same in both code bases, so it’s a good A vs B comparison.
>> 
>> In the Objective C version of the app, there are ~25,000 explicit references to self. (Keep in mind that this could easily have been a much bigger number if there weren’t such pervasive usage of ivars in the code.).
>> 
>> In the Swift version, there are ~1,000 explicit references to self, mostly in initializers and when passing self as an argument to a protocol — and about 10% of those would disappear with the proposal to allow implicit references to self with a strong capture list.
>> 
>> I know self is just a 4-letter word, and I know Swift’s goal isn’t to reduce character count simply for the sake of reducing character count, but it least for our project, avoiding “self”-blindness has really mode code more readable.
>> 
>> 
>>> On Dec 16, 2015, at 1:55 PM, Douglas Gregor via swift-evolution < <mailto:swift-evolution at swift.org>swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> Hello Swift community,
>>> 
>>> The review of “Require self for accessing instance members” begins now and runs through Sunday, December 20th. The proposal is available here:
>>> 
>>> 	 <https://github.com/apple/swift-evolution/blob/master/proposals/0009-require-self-for-accessing-instance-members.md>https://github.com/apple/swift-evolution/blob/master/proposals/0009-require-self-for-accessing-instance-members.md <https://github.com/apple/swift-evolution/blob/master/proposals/0009-require-self-for-accessing-instance-members.md>
>>> 
>>> Reviews are an important part of the Swift evolution process. All reviews should be sent to the swift-evolution mailing list at
>>> 
>>> 	 <https://lists.swift.org/mailman/listinfo/swift-evolution>https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>> 
>>> or, if you would like to keep your feedback private, directly to the review manager.
>>> 
>>> What goes into a review?
>>> 
>>> The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:
>>> 
>>> 	* What is your evaluation of the proposal?
>>> 	* Is the problem being addressed significant enough to warrant a change to Swift?
>>> 	* Does this proposal fit well with the feel and direction of Swift?
>>> 	* If you have you used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
>>> 	* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
>>> 
>>> More information about the Swift evolution process is available at
>>> 
>>> 	 <https://github.com/apple/swift-evolution/blob/master/process.md>https://github.com/apple/swift-evolution/blob/master/process.md <https://github.com/apple/swift-evolution/blob/master/process.md>
>>> 
>>> 	Cheers,
>>> 	Doug Gregor
>>> 	Review Manager
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>>  
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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


More information about the swift-evolution mailing list