Import concerns revisited

Walter Bright newshound at digitalmars.com
Sun Jul 9 01:55:56 PDT 2006


Ivan Senji wrote:
> There should be a big difference (if I understand things correctly):
> A simple example:
> 
> If I have:
> 
> module m1;
> 
> int func(){return 1;}
> 
> and
> 
> module mainmodule;
> 
> import m1;
> 
> void main()
> {
>   writefln(func());
>   writefln(m1.func());
> }
> 
> there are no problems with that!
> 
> But if I add:
> 
> module m2;
> 
> int func(){return 2;}
> 
> and change mainmodule to:
> 
> import m1;
> import m2;
> 
> void main()
> {
>   writefln(func());     //<- conflict
>   writefln(m1.func());
>   writefln(m2.func());
> }
> 
> And suddenly I get a lot of conflicts.

I agree, but the "static import" resolves that.


> Sure there is a way to solve it by adding:
> 
> alias m1.func func;
> 
> And then use m2.func to use func from m2.
> 
> But what if I wanted to give m2.func a totally different name to avoid
> confusion, i can try to add this:
> 
> alias m2.func someOtherName; instead of alias m1.func func;
> 
> But then once again I would get an error about a conflict:
> 
> Once again there is a solution:
> 
> alias m1.func func;
> alias m2.func f2;
> 
> And it works OK, but the intention here is a bit hidden.
> 
> Conclusion:
> 
> import m2.func as f2;
> 
> is not the same as:
> 
> import m2;
> alias m2.func f2;
> 
> but is equivalent to:
> 
> import m2;
> alias m2.func f2;
> //insert here a possibly huge number of aliases to avoid conflicts of
> m2.func with func from other imported modules.

If "static import" is used, there aren't any conflicts.

There are two issues here:

1) the "second class name lookup" characteristic of import.
2) inserting a name from one scope into another (possibly renaming it in 
the process)

static import does (1) by importing but disabling the second class lookup.
alias does (2).



More information about the Digitalmars-d mailing list