Version block "conditions" with logical operators
Artur Skawina via Digitalmars-d
digitalmars-d at puremagic.com
Wed May 11 09:12:49 PDT 2016
On 05/10/16 13:48, Tomer Filiba via Digitalmars-d wrote:
> On Tuesday, 10 May 2016 at 11:12:58 UTC, Tomer Filiba wrote:
>> Alternatively, an isVersion(x) predicate that I could use in a static if could do the trick
>
> Well, I've come up with
>
> template isVersion(string ver) {
> mixin(format(q{
> version(%s) {
> enum isVersion = true;
> }
> else {
> enum isVersion = false;
> }
> }, ver));
> }
>
> pragma(msg, isVersion!"foo"); // false
> pragma(msg, isVersion!"assert"); // true
>
> But it feels hackish too
It is a hack; the main problem being that the check happens at
the `isVersion` definition, and not where it's used. But, if you
only care about "global" identifiers, you can just use something
like this:
-----------------------------------------------------
module mver;
struct ver {
template opDispatch(string M) {
mixin(`
version(`~M~`) enum opDispatch = true;
else enum opDispatch = false;
`);
}
}
-----------------------------------------------------
import mver;
static if (ver.linux && !ver.D_LP64) {
/* ... */
}
-----------------------------------------------------
artur
More information about the Digitalmars-d
mailing list