Scope of variables
bearophile
bearophileHUGS at lycos.com
Fri Jun 24 18:10:47 PDT 2011
Adam D. Ruppe:
> > void baz(local int k) {
> > void inner(local int k) {}
> > }
>
> This is unneeded. By declaring a local variable, it's
> *obviously* local - there's no point in saying local again!
> If D was a bad language that allowed implicit variable definitions,
> it might make sense to do this, but we already declare all vars so
> it adds nothing.
You are right, but the purpose of that "local" annotation is a bit different: if you don't use that annotation, you define a local k and you already have a variable named k in an outer name space, the compiler is supposed to generate an error. So "local" is a way to say the compiler that you know there is an outer name "k" and you want to hide it.
Maybe "local" is not the best possible name for this annotation, "hider" seems more clear :-)
> What I've taken to doing is if I definitely want to access a
> class var, I'll just write this and use the dot to get to a global.
>
> That way, it's always clear what's going on without looking
> back at the function definition.
>
> I don't want that to be required though.
The purpose of the _optional_ @explicitLookup annotation by Nick Sabalausky is that one, to ask the compiler to enforce what you do.
Bye,
bearophile
More information about the Digitalmars-d
mailing list