Import concerns revisited

Walter Bright newshound at digitalmars.com
Tue Jul 11 03:05:46 PDT 2006


John Reimer wrote:
> I've seen this error also on several occasions.  I can't remember the 
> exact source of it, but I think it's related to another issue.  It was 
> my understanding that that was due to public imports somewhere up the 
> line. This might have been one of the reasons why discussions started 
> concerning a borked import system.  Perhaps I'm wrong?

How imports work is that first a name is searched for in the current 
namespace. If it is not found, then it is looked for in the import list. 
If it is found uniquely among the imports, then that is used. If it is 
in more than one import, an error occurs:

--- module A ---
void foo();
void bar();

--- module B ---
void foo();
void bar();

--- module C ---
import A;
void foo();
void test()
{ foo(); // C.foo() is called, it is found before imports are searched
   bar(); // A.bar() is called, since imports are searched
}

--- module D ---
import A;
import B;
void test()
{ foo();   // error, A.foo() or B.foo() ?
   A.foo(); // ok, call A.foo()
   B.foo(); // ok, call B.foo()
}

--- module E ---
import A;
import B;
alias B.foo foo;
void test()
{ foo();   // call B.foo()
   A.foo(); // call A.foo()
   B.foo(); // call B.foo()
}

And that's all there is to it. If you always used FQNs, you'll never run 
into any collision or ambiguity issues.

> Er.. that tells me more that there's a problem with the original 
> mechanism. And "static import" is a workaround.  Why not fix the 
> original import system?  There must be something wrong with it.  So are 
> we adding new constructs to workaround name clashes? Is that a good 
> idea?  Is that the reason 'static import' was suggested?  There 
> shouldn't be name clashes when fully qualified names are used in the 
> first place!

There aren't any name clashes if you use FQN's.


>> Which is not to say that I'm favouring 'static import' in this 
>> discussion, I'm simply saying that all of the proposals, including 
>> static import, are an *enormous* improvement over what we have now.

All 'static import' does is eliminate the "if the name is not found in 
the current module, look in the imports" step. This has the effect of 
requiring the use of FQN's when referencing imports.

Then in steps alias, which can be used to selectively bind import 
symbols into the current module namespace.



More information about the Digitalmars-d mailing list