www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Introduce: `__traits(version, IDENTIFIER)` to evaluate version

reply xoxorwr <xororwr gmail.com> writes:
I would like to use `version(IDENTIFIER)` directly within boolean 
expressions, but language rule doesn't allow it.

To demonstrate how convoluted the current workarounds are:

```D
version (WebAssembly)
{
     enum IO_RELOAD = false;
}
else
{
     version (CNoReload)
     {
         enum IO_RELOAD = false;
     }
     else
     {
         enum IO_RELOAD = true;
     }
}
```

I initially attempted to patch the compiler to support version in 
expressions, but the required changes are quite involved. This 
led to an alternative idea: why not reuse `__traits`?

Meaning: `__traits(version, IDENTIFIER)` would solve this cleanly 
by letting us write:

```D
enum IO_RELOAD = !__traits(version, WebAssembly) && 
!__traits(version, CNoReload);
```

This approach lets the user handle it rather than changing the 
language.
Jun 30
parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
It'll also need a debug variation of this too.

People in the past have done this using a string mixin and a template.

```d
template version_(string name) {
	mixin("version(name) enum version_ = true; else enum version_ = false;");
}
```

However yeah, a way that doesn't rely on templates or string mixins 
would be good.
Jun 30