[Issue 319] New: local variable can hide member variable

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Sep 2 20:04:31 PDT 2006


d-bugmail at puremagic.com wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=319
> 
>            Summary: local variable can hide member variable
>            Product: D
>            Version: 0.165
>           Platform: PC
>         OS/Version: Linux
>             Status: NEW
>           Severity: normal
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla at digitalmars.com
>         ReportedBy: benoit at tionex.de
> 
> 
> class C{
>     int mVal;
>     public this(){
>         int mVal = 2;
>     }
>     public void func(){
>         int mVal = 2;
>     }
> }
> 
> This compiles. But I think using a local variable, hiding a member variable
> should be illegal.
> 

I'm sorry but.... why?  One can easily disambiguate using the already provided 'this' 
referance, and there can be perfectly valid reasons to allow this (such as settors and 
obvious-use constructor parameters).

# class C {
#   int x ;
#
#   public void func () {
#     int x ;
#
#     x = 2;      // modify local variable
#     this.x = 2; // modify member variable
#   }
# }

A warning: maybe.  An error: for the love of D, no!

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-bugs mailing list