Local imports hide local symbols

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 23 13:24:21 PDT 2014


Andrei Alexandrescu:

> I've raised https://issues.dlang.org/show_bug.cgi?id=10378 to 
> critical. Please chime in with ideas for a good solution. 
> Thanks! -- Andrei

I think this code:

import foo;
void main() {}

should import only the "foo" identifier in the current 
module/scope (or fail to do so). This requires you to use foo.bar 
to access the name bar inside foo (and all the names inside the 
module are private on default, so you need to add a "public" to 
the ones you want to be visible outside the module). Plus if you 
want to avoid specifying the module/package in your module you 
can also use:

import foo: bar;

You can also do this, but this is discouraged:

import foo: *;

This last command is refused if some name shadowing happens.

When the module name clashes with a local name you have to use a 
renaming import:

void main(string[] args) {
     import margs = args;
}

But this design can't be used now in D. So I suggest to add anti 
hijacking logic similar to the with() command. When such name 
hiding errors are generated by a local import, the programmer has 
to list imported names, or use renamed imports.

Bye,
bearophile


More information about the Digitalmars-d mailing list