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

Jeremy Pereira jeremy.j.pereira at googlemail.com
Fri Dec 18 04:47:13 CST 2015


> On 17 Dec 2015, at 17:55, James Dempsey <dempsey at mac.com> wrote:
> 
> 
> 
> I don’t think I characterized the bugs as ‘sneaky and hard to find’.

Somebody in the thread characterised the bugs as “sneaky and hard to find” which is why I put quotes around it. Apologies for making it appear that you wrote it.


>> 
>> There are other ways to achieve the goal, for example Java has a warning that explicitly tells you when your local variable is shadowing an instance variable. Why wouldn’t that work? 
> 
> If there is a warning, then you would need to change the shadowing name to get rid of the warning, because you don’t want to ship with warnings, which seems too limiting.  Or you need to turn off the warning globally or for a particular file, but that means you can run into the problem accidentally in that context.

I don’t have a problem with renaming my local variables when they shadow instance variables in Java which happens rarely. If there isn’t a natural different name, I take that as a sign that maybe I don’t need both the local variable and the instance variable. 

Java also has the option to suppress a warning for an individual function or even variable declaration through annotations.

If the objection to a bit of renaming is that it is quite onerous, generally speaking I’d rather have to rename a couple of local variables occasionally to having to put “self.” on the front of every single use of a property.

> 
> But overall, looking at the Swift API guidelines, the primary concern is clarity at the call site, with clarity taking precedence over brevity. Making self mandatory is definitely clearer at the call site, but it is less brief.
> 

I dispute the last sentence. I think it adds unnecessary boilerplate that can obscure the intent of the code. I don’t think it adds clarity except in a few situations.

> So, I think that my personal preference happens to line up with those guidelines in this case.
> 
> I don’t think we are going to change each other’s mind about this proposal, but I understand your point of view.

You’re right about that :) I have absolutely no objection whatsoever to adding compiler support for your coding style. i.e. an optional warning tat you missed “self.” off a property reference. If you want to put “self.” in front of everything, I’m fine with that, just don’t make me do it too.


> 
> James
> 
> 



More information about the swift-evolution mailing list