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;

Reply via email to