| |
| Posted by Gregor Richards in reply to Chris Miller | PermalinkReply |
|
Gregor Richards
Posted in reply to Chris Miller
| 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
|