On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
I want to know if a library is present, and automatically disable non-vital features if it isn't.

I'd like it too... here's what I tried. It doesn't
work, though.

===
void libraryTest(string lib)() { mixin("import " ~ lib ~ ";"); }

template libraryExists(string lib) {
enum libraryExists = __traits(compiles, libraryTest!lib());
}

void main() {
       static if(libraryExists!"arsd.cgi") {
               import arsd.cgi;
               auto cgi = new Cgi();
               cgi.write("hello w/ cgi");
       } else {
               import std.stdio;
               writeln("hello w/ stdio");
       }
}
===


That's the idea, but I couldn't get it to actually work.
Imports are apparently done by the compiler before
anything else (which makes sense, really) and that breaks
the if compiles thing; it fails before it gets there. If
you move the libraryTest inline to the compiles thing, it
never uses the lib, so that's no good either.


Alas.

Reply via email to