www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - nice library workaround for lack of version boolean operations, would

reply Timothee Cour <thelastmammoth gmail.com> writes:
Just figured out we can do this. Could this be added to phobos?

----
//in std.compiler (or std.traits?)
template Version(alias V){
mixin(`
version(`~V~`){
enum Version=true;
}
else
enum Version=false;
`);
}
----

usage:

----
import std.compiler;
void main(){
static if(!Version!"assert")
writeln("not assert");

static if(Version!"OSX" && !Version!"D_NoBoundsChecks" || !Version!"assert")
        {
writeln("special code");
}
}
----

without this, we have to resort to something ugly, not dry, error prone:

----
//pollutes namespace, as we can't define version=temp inside a function
version(OSX)
{
version(D_NoBoundsChecks)
        {
}
else
        {
version=temp;//need to make sure temp doesn't clash with other version
identifiers / symbols
}
}
else
{
version(assert)
        {
}
else
        {
version=temp;//NOT DRY: repeats temp
}
}

void main()
{
version(assert)
        {
}
else
        {
writeln("not assert");
}

version(temp) // here we use it
        {
writeln("special code");
}
}
----


Likewise, with debug:
----
template Debug(alias V){
import std.traits:isIntegral;
static if(!isIntegral!(typeof(V))){
mixin(`
debug(`~V~`){
enum Debug=true;
}
else
enum Debug=false;
`);
}
else{
import std.conv:to;
mixin(`
debug(`~V.to!string~`){
enum Debug=true;
}
else
enum Debug=false;
`);
/+
 //NOTE:maybe a dmd bug but this didn't work
 debug(V)
   enum Debug=true;
 else
   enum Debug=false;
 +/
}
}
----


usage:
----
void main(){
        import std.compiler;
static if(Debug!2){
writeln("debug>=2");
}
static if(Debug!"foo"){
writeln("debug=foo");
}
}
----
Jun 05 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Timothee Cour:

 Just figured out we can do this. Could this be added to phobos?
I don't think it will be added to Phobos because that feature was not desired in the language... Bye, bearophile
Jun 05 2013