import concerns (was Re: Historical language survey)

Walter Bright newshound at digitalmars.com
Fri Jul 7 13:56:47 PDT 2006


kris wrote:
> D imports an entire module, into the current namespace (or some 
> variation upon that). This means that any additions to the original 
> module have to be aware of the namespace usage of *any* module that 
> imports the original. Otherwise, a namespace collision will occur and 
> the combination will fail to compile. M3 import explicitly from each 
> module instead ~ you can't have such a collision. The value of that is 
> just as solid today as it was in 1989.
> 
> One might argue that with D, one should create new modules instead of 
> extending existing ones? That's a fair point until you consider that the 
> module namespace is limited to one file, and the 'friend' aspect is 
> limited to one module (private attributes being visible within the one 
> module). Thus, D suffers this problem in a notable manner.
> 
> I forget whether M3 supports importing into a distinct namespace or not 
> --- the "import x.y.z. as foo;" syntax -- but that can alleviate related 
> problems, and would help resolve the current D namespace conflicts that 
> are quite prevalant?

import namespaces are second class citizens in D - they are easily 
overridden by using aliases or fully qualified lookups:

import a;	// defines foo()
import b;	// defines foo()

foo();		// ambiguous
a.foo();	// doesn't matter if there's a b.foo
b.foo();	// works

alias a.foo foo;
foo();		// works

As for import x.y.z. as foo;, you can do:

alias x.y.z foo;
foo.bar();

alias x.y abc;
abc.x.bar();

alias x def;
def.y.z.bar();

The alias works at any level you choose to make it. Alias can be used to 
'import' any name into the current namespace, making it first class.

The second class lookup capability is to make it easier to write quick 
and dirty programs. Aliases or fully qualified names should be used when 
writing large, complex apps. Think of it like using private - you 
wouldn't bother with it for small or throwaway programs, but you 
wouldn't think of not using it for long lived or complex apps.



More information about the Digitalmars-d mailing list