Exclusive Imports
Michelle Long
HappyDance321 at gmail.com
Fri Jan 11 16:32:09 UTC 2019
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)
More information about the Digitalmars-d
mailing list