Actually I can improve syntax a bit as follows:<div><br></div><div><br></div><div>user code:</div><div>----</div><div>void main(){</div><div><div>static if(versions.OSX&& versions.D_NoBoundsChecks || !versions.has!"assert)</div>
<div> writeln("special code");</div></div><div>}</div><div>----</div><div><br></div><div>(note the alternate syntax with 'has' to support special keyword versions such as assert).</div><div>The only drawback is that I'd like to have instead:</div>
<div><div>static if(versions.OSX&& versions.D_NoBoundsChecks || !versions["assert"] )</div><div>any idea to support that?</div><div><br></div><div>library code:</div><div>---</div><div>//in std.compiler</div>
<div><div>enum versions=(){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>struct versions_{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>bool has(alias a)(){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>mixin(`</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>version(`~a~`){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return true;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>else </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return false;</div><div>`);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>alias has this;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>auto opDispatch(string a)(){</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>mixin(`</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>version(`~a~`){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return true;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>else </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return false;</div>
<div>`);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return versions_.init;</div>
<div>}();</div></div><div><div>---</div><div></div></div><div><br></div><div><br></div><div><br><br><div class="gmail_quote">On Wed, Jun 5, 2013 at 2:28 AM, Timothee Cour <span dir="ltr"><<a href="mailto:thelastmammoth@gmail.com" target="_blank">thelastmammoth@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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 style="white-space:pre-wrap"> </span>mixin(`</div>
<div><span style="white-space:pre-wrap"> </span>version(`~V~`){</div><div><span style="white-space:pre-wrap"> </span>enum Version=true;</div><div><span style="white-space:pre-wrap"> </span>}</div>
<div><span style="white-space:pre-wrap"> </span>else </div><div><span style="white-space:pre-wrap"> </span>enum Version=false;</div><div>`);</div><div>}</div></div><div>----</div><div>
<div><br></div></div><div>usage:</div><div><br></div><div>----</div><div>import std.compiler;</div><div><div>void main(){</div><div><span style="white-space:pre-wrap"> </span>static if(!Version!"assert")</div>
<div><span style="white-space:pre-wrap"> </span>writeln("not assert");</div><div><br></div><div><span style="white-space:pre-wrap"> </span>static if(Version!"OSX" && !Version!"D_NoBoundsChecks" || !Version!"assert")</div>
<div> {</div><div><span style="white-space:pre-wrap"> </span>writeln("special code");</div><div><span style="white-space:pre-wrap"> </span>}</div><div>}</div></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't define version=temp inside a function</div>
<div>version(OSX)</div><div>{</div><div><span style="white-space:pre-wrap"> </span>version(D_NoBoundsChecks)</div><div> {</div><div><span style="white-space:pre-wrap"> </span>}</div>
<div><span style="white-space:pre-wrap"> </span>else</div><div> {</div><div><span style="white-space:pre-wrap"> </span>version=temp;//need to make sure temp doesn't clash with other version identifiers / symbols</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div>}</div><div>else</div><div>{</div><div><span style="white-space:pre-wrap"> </span>version(assert)</div><div> {</div><div>
<span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div> {</div><div><span style="white-space:pre-wrap"> </span>version=temp;//NOT DRY: repeats temp</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div>}</div><div><br></div><div>void main()</div><div>{</div><div><span style="white-space:pre-wrap"> </span>version(assert)</div>
<div> {</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div> {</div><div><span style="white-space:pre-wrap"> </span>writeln("not assert");</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>version(temp) // here we use it</div><div> {</div><div><span style="white-space:pre-wrap"> </span>writeln("special code");</div>
<div><span style="white-space:pre-wrap"> </span>}</div><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 style="white-space:pre-wrap"> </span>import std.traits:isIntegral;</div><div><span style="white-space:pre-wrap"> </span>static if(!isIntegral!(typeof(V))){</div><div><span style="white-space:pre-wrap"> </span>mixin(`</div>
<div><span style="white-space:pre-wrap"> </span>debug(`~V~`){</div><div><span style="white-space:pre-wrap"> </span>enum Debug=true;</div><div><span style="white-space:pre-wrap"> </span>}</div>
<div><span style="white-space:pre-wrap"> </span>else </div><div><span style="white-space:pre-wrap"> </span>enum Debug=false;</div><div>`);</div><div><span style="white-space:pre-wrap"> </span>}</div>
<div><span style="white-space:pre-wrap"> </span>else{</div><div><span style="white-space:pre-wrap"> </span>import std.conv:to;</div><div><span style="white-space:pre-wrap"> </span>mixin(`</div>
<div><span style="white-space:pre-wrap"> </span>debug(`~V.to!string~`){</div><div><span style="white-space:pre-wrap"> </span>enum Debug=true;</div><div><span style="white-space:pre-wrap"> </span>}</div>
<div><span style="white-space:pre-wrap"> </span>else </div><div><span style="white-space:pre-wrap"> </span>enum Debug=false;</div><div>`);</div><div><span style="white-space:pre-wrap"> </span>/+</div>
<div><span style="white-space:pre-wrap"> </span> //NOTE:maybe a dmd bug but this didn't work</div><div><span style="white-space:pre-wrap"> </span> debug(V)</div><div><span style="white-space:pre-wrap"> </span> enum Debug=true;</div>
<div><span style="white-space:pre-wrap"> </span> else </div><div><span style="white-space:pre-wrap"> </span> enum Debug=false;</div><div><span style="white-space:pre-wrap"> </span> +/</div>
<div><span style="white-space:pre-wrap"> </span>}<span style="white-space:pre-wrap"> </span></div><div>}</div></div><div><div>----</div><div><br></div><div><br></div><div>usage:</div><div>
----</div><div><div>void main(){</div><div> import std.compiler;</div><div><span style="white-space:pre-wrap"> </span>static if(Debug!2){</div><div><span style="white-space:pre-wrap"> </span>writeln("debug>=2");</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>static if(Debug!"foo"){</div><div><span style="white-space:pre-wrap"> </span>writeln("debug=foo");</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div>}</div></div><div><div>----</div><div><br></div><div></div></div><div></div></div><div></div></div>
</blockquote></div><br></div></div>