Thread overview
import all except specified symbols: eg import std.stdio:!writeln,write;
Sep 09, 2012
timotheecour
Sep 10, 2012
Jonathan M Davis
Sep 10, 2012
Jacob Carlborg
Sep 10, 2012
Artur Skawina
September 09, 2012
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?
September 10, 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
September 10, 2012
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
September 10, 2012
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