Missing compiler warning?

Chris wendlec at tcd.ie
Sat Oct 19 09:50:16 PDT 2013


On Friday, 18 October 2013 at 17:42:22 UTC, Jonathan M Davis 
wrote:
> On Friday, October 18, 2013 18:32:32 bearophile wrote:
>> Currently D doesn't give an error or warning if in a method you
>> declare a local variable with the same name of a instance 
>> member.
>> This is indeed a source of bugs and I have had similar 
>> problems.
>> Generally D prefers to minimize the number of warnings, so
>> according to the D style that should become an error.
>
> There's no way that that's ever going to become an error. It's 
> just too
> useful. The issue has come up before, and it's not going away. 
> The prime
> example is constructors.
>
> this(int a, string b)
> {
>  this.a = a;
>  this.b = b;
>  ...
> }
>
> Too many people would get annoyed if they were required to name 
> their local
> variables differently - especially in a case like this. If you 
> want to avoid
> the problem, then just don't name your local variables the same 
> as your member
> variables - the most obvious solution being to do name your 
> member variables
> differently - e.g. by prepending them with _.
>
> - Jonathan M Davis

A warning would be enough. The thing is I didn't want to give it 
the same name. It was meant to be the class variable but the auto 
was a leftover from a test. A warning would have been nice, à la 
"do you really want this?". I would have seen it immediately.


More information about the Digitalmars-d mailing list