On 01/30/2012 04:18 PM, Adam D. Ruppe wrote:
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.

The fact that this does not work is a compiler bug:

static if(is(typeof({import asdf;}))){}

it fails with exit code 1 without spitting out an error message.
Once that is fixed, it could be used to detect presence or absence of a library.

http://d.puremagic.com/issues/show_bug.cgi?id=7399

Reply via email to