Thread overview
Variable no-shadowing problems
Dec 22, 2006
Chris Miller
Dec 23, 2006
Gregor Richards
Dec 23, 2006
Chris Miller
Dec 23, 2006
Xinok
December 22, 2006
To those not sure what shadowing means, here is an example of variable shadowing:
   int i;
   {
      int i;
   }
The inner `i` variable shadows the outer one.


D has made such shadowing illegal and flags it as an error. I'm against this restriction, but I have a compromise.

I am proposing a `shadow` keyword be added to D. Actually, the general idea is from someone else from #D IRC chat, but I have several reasons why explained below.

1) Accidentally shadowing a variable. If this happens you will have to go back and manually fix up all uses of the shadowed name, which has a very high chance of error. Forget to fix one and it goes by silently; bug. Or, simply add `shadow` to its declaration, avoiding tedious, error-prone fixing, and make people aware of what's going on.

2) Adding code. You can simply add a { } block and declare at will. With no shadowing, I am tempted to append random numbers to my variable names so I don't run into this, or moreso tempted to reuse variable names like is popular from C, as in declare once at the top of the function and reuse the variable for different things.

3) Code generators emitting D source code. They may not wish to keep track of such shadowing and could simply mark all declarations as `shadow`.


Notes:

`shadow` does not allow redeclarations in the exact same scope (although perhaps it could).

As for reason (1), if you are personally against shadowing, you are not forced to use the keyword and can continue treating it how it is done now.

- Chris
December 23, 2006
Chris Miller wrote:
> To those not sure what shadowing means, here is an example of variable  shadowing:
>    int i;
>    {
>       int i;
>    }
> The inner `i` variable shadows the outer one.
> 
> 
> D has made such shadowing illegal and flags it as an error. I'm against  this restriction, but I have a compromise.
> 
> I am proposing a `shadow` keyword be added to D. Actually, the general  idea is from someone else from #D IRC chat, but I have several reasons why  explained below.
> 
> 1) Accidentally shadowing a variable. If this happens you will have to go  back and manually fix up all uses of the shadowed name, which has a very  high chance of error. Forget to fix one and it goes by silently; bug. Or,  simply add `shadow` to its declaration, avoiding tedious, error-prone  fixing, and make people aware of what's going on.

I was going to respond to this somehow, but I don't think I really have a response ... IMHO this doesn't actually cut down the debugging time at all, since in all likelihood coder-X will have inadvertantly mixed-and-matched in the inner scope. Maybe that's not true, I don't know :)

> 
> 2) Adding code. You can simply add a { } block and declare at will. With  no shadowing, I am tempted to append random numbers to my variable names  so I don't run into this, or moreso tempted to reuse variable names like  is popular from C, as in declare once at the top of the function and reuse  the variable for different things.

I think this scenario could be more cleanly filed under "code reuse." If you copy/paste code from one project to another, with shadowing you don't have to worry (too much) about variable collision.

> 
> 3) Code generators emitting D source code. They may not wish to keep track  of such shadowing and could simply mark all declarations as `shadow`.

This I agree with entirely.

> 
> 
> Notes:
> 
> `shadow` does not allow redeclarations in the exact same scope (although  perhaps it could).

Redeclaration in the same scope ... hmmmmmm ... makes me gag a bit.

> 
> As for reason (1), if you are personally against shadowing, you are not  forced to use the keyword and can continue treating it how it is done now.

Yeah, if you never plan on reading anybody else's code ;)

> 
> - Chris

My opinion in short: I don't agree with 1, I somewhat agree with 2, I agree entirely with 3. You've covered the full gambit, bravo ;)

 - Gregor Richards
December 23, 2006
For some reason the following post isn't showing up in my news reader, http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=45708
December 23, 2006
It may not be the most desirable solution, but you could create a nested function to 'shadow' names.

int main(){
	int i;
	void func(){
		int i;
	}
	func();
}