Thread overview
Exclusive Imports
Jan 10
. .
January 10
Is there a way to import all symbols from a module, excluding one symbol?

For example, lets say I am using std.file and std.stdio in the same scope (both of which have a symbol "write"). I need to use the "write" from std.file. I am using *a lot* of different functions from both std.stdio and std.file, so a selective import for everything except "write" would be too long and inefficient. I need to import everything from std.stdio *except* "write". I was hoping something like this was possible: `import std.stdio : !write;`. I also was thinking about just doing a renamed import and renaming std.stdio.write to some other name that will never be used, but that is not such a good idea because it still pollutes the namespace.

Is there a better way to do exclusive imports? If not, any thoughts on adding exclusive imports to D?
January 10
On Thursday, 10 January 2019 at 21:37:01 UTC, . . wrote:
> Is there a way to import all symbols from a module, excluding one symbol?

No, but you can import both and then specify what you want the local one to be with an alias along with the imports:

import std.stdio;
import std.file;

alias write = std.stdio.write; // specify what I mean...

void main() {
        write("foo", "bar"); // would be ambiguous if not for the alias
}
January 11
On Thursday, 10 January 2019 at 21:37:01 UTC, . . wrote:
> Is there a way to import all symbols from a module, excluding one symbol?
>
> For example, lets say I am using std.file and std.stdio in the same scope (both of which have a symbol "write"). I need to use the "write" from std.file. I am using *a lot* of different functions from both std.stdio and std.file, so a selective import for everything except "write" would be too long and inefficient. I need to import everything from std.stdio *except* "write". I was hoping something like this was possible: `import std.stdio : !write;`. I also was thinking about just doing a renamed import and renaming std.stdio.write to some other name that will never be used, but that is not such a good idea because it still pollutes the namespace.
>
> Is there a better way to do exclusive imports? If not, any thoughts on adding exclusive imports to D?

Yes, sorta:

struct from(string namespace) {
    template opDispatch(string subnamespace) {
        mixin("import opDispatch = "~namespace~"."~subnamespace~";");
    }
}

alias std = from!"std";


struct _std
{
   template opDispatch(string moduleName)
   {
     mixin("import opDispatch = std." ~ moduleName ~ ";");
   }
}

You could use one of those and then do some check to prevent the ones you don't want...

You will have to explicitly specify the import though.


You could write some code that automatically publicly imports all the imports except the ones you exclude and then import that module.
(sorta like doing it manually)