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:
--20cf307d04384f45c104de64d1b5
Content-Type: text/plain; charset=ISO-8859-1

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");
}
}
----

--20cf307d04384f45c104de64d1b5
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>Just figured out we can do this. Could this be added to phobos?</div><=
div><br></div><div>----</div><div>//in std.compiler (or std.traits?)</div><=
div><div>template Version(alias V){</div><div><span class=3D"Apple-tab-span=
" style=3D"white-space:pre">	</span>mixin(`</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>versi=
on(`~V~`){</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pr=
e">		</span>enum Version=3Dtrue;</div><div><span class=3D"Apple-tab-span" s=
tyle=3D"white-space:pre">	</span>}</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>else=
=A0</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</=
span>enum Version=3Dfalse;</div><div>`);</div><div>}</div></div><div>----</=
div><div>
<div><br></div></div><div>usage:</div><div><br></div><div>----</div><div>im=
port std.compiler;</div><div><div>void main(){</div><div><span class=3D"App=
le-tab-span" style=3D"white-space:pre">	</span>static if(!Version!&quot;ass=
ert&quot;)</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>writ=
eln(&quot;not assert&quot;);</div><div><br></div><div><span class=3D"Apple-=
tab-span" style=3D"white-space:pre">	</span>static if(Version!&quot;OSX&quo=
t; &amp;&amp; !Version!&quot;D_NoBoundsChecks&quot; || !Version!&quot;asser=
t&quot;)</div>
<div>=A0 =A0 =A0 =A0 {</div><div><span class=3D"Apple-tab-span" style=3D"wh=
ite-space:pre">		</span>writeln(&quot;special code&quot;);</div><div><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</div><div>}</d=
iv></div><div>
----</div><div><br></div><div>without this, we have to resort to something =
ugly, not dry, error prone:</div><div><br></div><div>----</div><div><div>//=
pollutes namespace, as we can&#39;t define version=3Dtemp inside a function=
</div>
<div>version(OSX)</div><div>{</div><div><span class=3D"Apple-tab-span" styl=
e=3D"white-space:pre">	</span>version(D_NoBoundsChecks)</div><div>=A0 =A0 =
=A0 =A0 {</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">	</span>}</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>else<=
/div><div>=A0 =A0 =A0 =A0 {</div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">		</span>version=3Dtemp;//need to make sure temp doesn=
&#39;t clash with other version identifiers / symbols</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</di=
v><div>}</div><div>else</div><div>{</div><div><span class=3D"Apple-tab-span=
" style=3D"white-space:pre">	</span>version(assert)</div><div>=A0 =A0 =A0 =
=A0 {</div><div>
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</div><di=
v><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>else</di=
v><div>=A0 =A0 =A0 =A0 {</div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">		</span>version=3Dtemp;//NOT DRY: repeats temp</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</di=
v><div>}</div><div><br></div><div>void main()</div><div>{</div><div><span c=
lass=3D"Apple-tab-span" style=3D"white-space:pre">	</span>version(assert)</=
div>
<div>=A0 =A0 =A0 =A0 {</div><div><span class=3D"Apple-tab-span" style=3D"wh=
ite-space:pre">	</span>}</div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>else</div><div>=A0 =A0 =A0 =A0 {</div><div><span c=
lass=3D"Apple-tab-span" style=3D"white-space:pre">		</span>writeln(&quot;no=
t assert&quot;);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</di=
v><div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-space:p=
re">	</span>version(temp) // here we use it</div><div>=A0 =A0 =A0 =A0 {</di=
v><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>wr=
iteln(&quot;special code&quot;);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</di=
v><div>}</div></div><div><div>----</div><div><br></div><div><br></div><div>=
Likewise, with debug:</div><div>----</div><div><div>template Debug(alias V)=
{</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>impor=
t std.traits:isIntegral;</div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>static if(!isIntegral!(typeof(V))){</div><div><spa=
n class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>mixin(`</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>debug=
(`~V~`){</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"=
		</span>enum Debug=3Dtrue;</div><div><span class=3D"Apple-tab-span" style=

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>else= =A0</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </= span>enum Debug=3Dfalse;</div><div>`);</div><div><span class=3D"Apple-tab-s= pan" style=3D"white-space:pre"> </span>}</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>else{= </div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </spa= n>import std.conv:to;</div><div><span class=3D"Apple-tab-span" style=3D"whi= te-space:pre"> </span>mixin(`</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>debug= (`~V.to!string~`){</div><div><span class=3D"Apple-tab-span" style=3D"white-= space:pre"> </span>enum Debug=3Dtrue;</div><div><span class=3D"Apple-tab-s= pan" style=3D"white-space:pre"> </span>}</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>else= =A0</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </= span>enum Debug=3Dfalse;</div><div>`);</div><div><span class=3D"Apple-tab-s= pan" style=3D"white-space:pre"> </span>/+</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span> //N= OTE:maybe a dmd bug but this didn&#39;t work</div><div><span class=3D"Apple= -tab-span" style=3D"white-space:pre"> </span> debug(V)</div><div><span cla= ss=3D"Apple-tab-span" style=3D"white-space:pre"> </span>=A0 =A0enum Debug= =3Dtrue;</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span> els= e=A0</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> <= /span>=A0 =A0enum Debug=3Dfalse;</div><div><span class=3D"Apple-tab-span" s= tyle=3D"white-space:pre"> </span> +/</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>}<spa= n class=3D"Apple-tab-span" style=3D"white-space:pre"> </span></div><div>}<= /div></div><div><div>----</div><div><br></div><div><br></div><div>usage:</d= iv><div> ----</div><div><div>void main(){</div><div>=A0 =A0 =A0 =A0 import std.compi= ler;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </= span>static if(Debug!2){</div><div><span class=3D"Apple-tab-span" style=3D"= white-space:pre"> </span>writeln(&quot;debug&gt;=3D2&quot;);</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>}</di= v><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>sta= tic if(Debug!&quot;foo&quot;){</div><div><span class=3D"Apple-tab-span" sty= le=3D"white-space:pre"> </span>writeln(&quot;debug=3Dfoo&quot;);</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>}</di= v><div>}</div></div><div><div>----</div><div><br></div><div></div></div><di= v></div></div><div></div></div> --20cf307d04384f45c104de64d1b5--
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