public import and bugs it causes

Walter Bright newshound1 at digitalmars.com
Fri May 14 21:05:51 PDT 2010


Steven Schveighoffer wrote:
> C# sucks when importing conflicting names.  I've dealt with it, and you 
> simply have to refer to the fully qualified name to be unambiguous.  
> With D, I can rename the less used one at import time to only have to 
> rewrite some of them.  This does not ruin the experience for anyone else 
> who uses only one of the modules.  Note that D refuses to compile 
> ambiguous symbols, so the problem is not that it blindly chooses one.

D actually does one better for functions of the same name appearing in multiple 
imports. It will still compile them as long as the overloads do not conflict, 
i.e. the space of arguments each will accept is disjoint. Only if the overloads 
conflict is an ambiguity error generated.

For example:

--- module a ---
void foo(long i);
--- module b ---
void foo(ulong i);
void foo(char[3] a);
----------------

import a, b;

foo(1);   // error, ambiguous
char[3] a;
foo(a);   // no problem, chooses b.foo(char[3])

-----------------

I don't know any other language that handles this nearly as nicely.

As for misspelling a name where the misspelling matches a name in another scope, 
or not declaring a name and that name matches a name in another scope, I don't 
see any way the compiler could detect such mistakes.


More information about the Digitalmars-d mailing list