How to check that import module will succeed?
evilrat
evilrat666 at gmail.com
Fri Jul 26 06:24:18 UTC 2019
On Friday, 26 July 2019 at 03:42:58 UTC, Andrey Zherikov wrote:
> Is there a way to check whether some module, say "foo", is
> available for import before doing "import foo"?
I did some really retarded utility like this in the past, worked
for me, but I can't say it is that well tested and there might be
a better way (after all it just assumes that template
instantiation failure can only mean there is no such module), so
in short template allowed to fail, and we check if it is failed
or not to test if desired module exists. Don't remember if static
is really necessary or you can just return directly.
bool isModuleAvailable(alias modName)() {
mixin("import " ~ modName ~ ";");
static if (__traits(compiles, mixin(modName).stringof))
return true;
else
return false;
}
// use like this
static if (__traits(compiles, isModuleAvailable!"mymod" ))
import mymod;
More information about the Digitalmars-d-learn
mailing list