On Sunday, 15 March 2015 at 18:03:55 UTC, Robert M. Münch wrote:
On 2015-03-15 17:36:24 +0000, Robert M. Münch said:
Is there a way to use "version(...)" to have code sections depending on compiler version? Something like:

version(dmd >= 2.067) or version(dmd < 2.067)?

Answerting myself:

static if (__traits(compiles, version_minor < 67))
 import std.string; // format() for versions < 2.0.67
else
 import std.format; // format() for versions >= 2.0.67

That doesn't do what you want.

You need to `import std.compiler;` for version_minor. Without that import, `__traits(compiles, version_minor < 67)` is always false, because version_minor is undefined.

And if you add the import, `__traits(compiles, version_minor < 67)` is always true, no matter the value of version_minor. Use `static if(version_minor < 67)` instead.

Also, if you check version_minor, it's probably a good idea to check (or static assert) version_major, too.

Finally, there's need for this (yet). `std.string.format` is fine with 2.067, too. So unless you're going for (far) future compatiblity, you can just do `import std.string;`.

Reply via email to