the point of selective importing
Ameer Armaly
ameer_armaly at hotmail.com
Tue Jul 11 06:22:59 PDT 2006
"Bill Baxter" <Bill_member at pathlink.com> wrote in message
news:e8v32s$65i$1 at digitaldaemon.com...
> 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.
>
Why? Isn't the point of fqn importing to prefix the module name? This goes
back to the idea that either you import the module in to the global
namespace or you deal with the full name; I just don't see this being done
very often, especially with well known libraries.
> 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.
>
Why not then just import the whole module in to the namespace? It's not as
if the namespace is something you actually look and see "god this is
cluttered with junk," so I really don't see the point. If a module is
written in such a way as to force you to go to these lengths to optimally
use it then it's badly written.
> 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