import all except specified symbols: eg import std.stdio:!writeln,write;

Jonathan M Davis jmdavisProg at gmx.com
Sun Sep 9 17:07:19 PDT 2012


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


More information about the Digitalmars-d-learn mailing list