www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Exclusive Imports

reply . . <a b.c> writes:
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?
Jan 10
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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 }
Jan 10
prev sibling parent Michelle Long <HappyDance321 gmail.com> writes:
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)
Jan 11