Thread overview
[Issue 319] New: local variable can hide member variable
Sep 02, 2006
d-bugmail
Sep 02, 2006
d-bugmail
Sep 03, 2006
Bruno Medeiros
Sep 03, 2006
d-bugmail
September 02, 2006
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@digitalmars.com
        ReportedBy: benoit@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.


-- 

September 02, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=319


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Priority|P2                          |P3




------- Comment #1 from bugzilla@digitalmars.com  2006-09-02 16:38 -------
This is an enhancement request.


-- 

September 03, 2006
d-bugmail@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@digitalmars.com
>         ReportedBy: benoit@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
September 03, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=319


benoit@tionex.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #3 from benoit@tionex.de  2006-09-03 04:41 -------
Sorry, I thought it is comparable with the feature from dmd 0.161: "Shadowing local variable declarations is now deprecated."

So I set the status to INVALID.


-- 

September 03, 2006
Chris Nicholson-Sauls wrote:
> d-bugmail@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@digitalmars.com
>>         ReportedBy: benoit@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

I often end up using stuff like that in constructors:

  class C {
    int x ;

    public this (int x) {
      this.x = x;
    }
  }

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D