www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - import all except specified symbols: eg import

reply "timotheecour" <thelastmammoth gmail.com> writes:
I'd like to have something like:
---
import std.stdio:!writeln,write;
---
which would import all symbols from std.stdio except the ones 
listed (writeln,write).

Use case:
The reason is to avoid writing verbose code (specifying all 
symbols to import except those 2), example when writing a module 
(eg overrides.stdio) which overrides just those 2 symbols (eg for 
logging to a file each call to write,writeln) but keeps the rest 
intact.

Is there a way or can that be implemented?
Sep 09 2012
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, September 10, 2012 01:33:17 timotheecour wrote:
 I'd like to have something like:
 ---
 import std.stdio:!writeln,write;
 ---
 which would import all symbols from std.stdio except the ones
 listed (writeln,write).
 
 Use case:
 The reason is to avoid writing verbose code (specifying all
 symbols to import except those 2), example when writing a module
 (eg overrides.stdio) which overrides just those 2 symbols (eg for
 logging to a file each call to write,writeln) but keeps the rest
 intact.
 
 Is there a way or can that be implemented?

Listing specific functions to import really only makes sense when you only need a few of them. There is no way to say _not_ to import something from a module when importing it. You can either import it all, or import specific symbols from it. What you're asking to do, can't really bo done. You could create a new module that publicly imports all of the symbols that you want and not the ones that you don't. Then have your existing module import that one. But that just moves the verboseness to another module, which _could_ save you some typing if you're doing the same thing in multiple modules, or it could be juts moving the problem to another module, making things even _more_ verbose. The normal thing to do is to simply use the whole import path when there's a conflict. e.g. std.ascii.isAlpha(var1); std.uni.isAlpha(var2); If you want to make it less verbose, you can rename the import import stuff = my.really.long.import.path stuff.func(5); And there's always alias, alias std.ascii.isAlpha isAlpha; but be aware that that will affect every module which imports yours (even if you make the alias private - since private effects access, not overload resolution), so it's generally a bad idea to use an alias like that unless you name it something completely different. e.g. private alias std.ascii.isAlpha isOmega; - Jonathan M Davis
Sep 09 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-09-10 02:07, Jonathan M Davis wrote:

 You could create a new module that publicly imports all of the symbols that
 you want and not the ones that you don't. Then have your existing module
 import that one. But that just moves the verboseness to another module, which
 _could_ save you some typing if you're doing the same thing in multiple
 modules, or it could be juts moving the problem to another module, making
 things even _more_ verbose.

 The normal thing to do is to simply use the whole import path when there's a
 conflict. e.g.

 std.ascii.isAlpha(var1);
 std.uni.isAlpha(var2);

 If you want to make it less verbose, you can rename the import

 import stuff = my.really.long.import.path

 stuff.func(5);

 And there's always alias,

 alias std.ascii.isAlpha isAlpha;

 but be aware that that will affect every module which imports yours (even if
 you make the alias private - since private effects access, not overload
 resolution), so it's generally a bad idea to use an alias like that unless you
 name it something completely different. e.g.

 private alias std.ascii.isAlpha isOmega;

If we're just thinking from a technical view it might be possible. Something like: mixin(importExcept!(std.stdio, "write", "writeln")); If it's possible to get all symbols from another module then it's easy to just exclude a few symbols and create imports for the rest. -- /Jacob Carlborg
Sep 09 2012
prev sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 09/10/12 01:33, timotheecour wrote:
 I'd like to have something like:
 ---
 import std.stdio:!writeln,write;
 ---
 which would import all symbols from std.stdio except the ones listed
(writeln,write).
 
 Use case:
 The reason is to avoid writing verbose code (specifying all symbols to import
except those 2), example when writing a module (eg overrides.stdio) which
overrides just those 2 symbols (eg for logging to a file each call to
write,writeln) but keeps the rest intact.
 
 Is there a way or can that be implemented?

import std.stdio; float writeln(double a) { return a*a; } void main() { writeln(3.14); writeln("original no longer accesible"); } artur
Sep 10 2012