Scope/block behaviour

bearophile bearophileHUGS at lycos.com
Thu Aug 19 07:11:14 PDT 2010


Andrei Alexandrescu:

> Allowing global symbol masking
> is necessary  for writing  good modular code  that's assembled  out of
> separately compiled  parts; you  don't want the  addition of  a global
> variable to suddenly  render various innocent bystanders uncompilable.

When you import modules you need to ask for what you want, and not receive a lot of names you don't want. So if you add one variable and you don't import it, it shall not give the problems you say.

I'd like a tidier management of global variable names. So I am not sure I agree with what is written there.

Currently DMD accepts code like:

int x = 1;
// ... lot of code here
void foo() {
    x++; // bug, local x undefined
    // ... more code here
    int x;
    x++;
}
void main() {}


Generally in a system a big source of unwanted complexity comes from unwanted interactions between its subsystems. So I'd like D functions to be a bit more strict in where they look for names. Global variables are sometimes useful and I don't want to remove them from the language, as I think the Delight language has done. But I'd like them to be asked for in a more explicit way inside functions (in Python there is the 'global' attribute for this, but its semantics is not tidy. In D there is the leading "." to denote an outer name, but its usage is optional unless an equal local name exists).

Bye,
bearophile


More information about the Digitalmars-d mailing list