the point of selective importing

Bill Baxter Bill_member at pathlink.com
Mon Jul 10 19:40:28 PDT 2006


In article <e8urqt$2vb2$1 at digitaldaemon.com>, Ameer Armaly says...
>...
>It might just be me, but IMHO you're trying to mix apples with oranges; 
>either you want fully qualified imports or you want standard imports.  If 
>you desperately want to mix them, then I agree with Walter in that aliases 
>should be just fine; anyone with half a brain will put them right near the 
>import where they can be seen; anyone who doesn't do that is just a bad 
>coder.

Good points. 

But 
A) renaming modules is pretty common once you allow FQN import.

static import module.with.a.long.name.string;
alias module.with.a.long.name.string Str;

It would be nice to be able to put those into one statement so that the module
name doesn't have to be typed in twice.

B) Sometimes you want to keep your namespace clean except for a handful of
specific symbols from some module:

static import module.with.a.long.name;
alias module.with.a.long.name.foo Foo;
alias module.with.a.long.name.bar Bar;
alias module.with.a.long.name.baz Baz;

That would also be easier to read,write, and maintain if module.with.a.long.name
didn't appear 4 times.

Granted, you could do:
static import module.with.a.long.name;
alias module.with.a.long.name MWLN;
alias MWLN.foo Foo;
alias MWLN.bar Bar;
alias MWLN.baz Baz;

which is better, but the full module name still appears twice, and I didn't
really want MWLN or module.with.a.long.name in my namespace to begin with.

However, I'm starting to agree with Walter, on that one.  There probably aren't
too many times when you want to import specific symbols without also wanting
access to the module as a whole.  In the example I gave from my python code,
"from numpy import transpose as T", the truth is that I also do "import numpy as
num" in that code.  So I could have just said "T = num.transpose", which would
be the equivalent of D's "alias num.transpose T;". 

That still leaves the need for some sort of way to do:
static import module.with.a.long.name as MWLN;
in which MWLN is the *only* symbol introduced into the current namespace.
(Although I guess in the import-and-rename case 'static' would be redundant, or
just incorrect since the name *is* changing.)






More information about the Digitalmars-d mailing list