On Wed, Jul 10, 2013 at 7:16 PM, Timothee Cour <span dir="ltr"><<a href="mailto:thelastmammoth@gmail.com" target="_blank">thelastmammoth@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>template isCallableCTFE(alias fun){</div><div><span style="white-space:pre-wrap"> </span>template isCallableCTFE_aux(alias T){</div><div><span style="white-space:pre-wrap"> </span>enum isCallableCTFE_aux=T;</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>enum isCallableCTFE=__traits(compiles,isCallableCTFE_aux!(fun()));</div><div>}</div>
<div><br></div><div>template isCallableCTFE2(fun...){</div><div><span style="white-space:pre-wrap"> </span>enum isCallableCTFE2=true;</div><div>}</div><div><br></div><div><br></div><div>unittest{</div><div>
<span style="white-space:pre-wrap"> </span>int fun1(){</div><div><span style="white-space:pre-wrap"> </span>return 1;</div><div><span style="white-space:pre-wrap"> </span>}</div>
<div><span style="white-space:pre-wrap"> </span>auto fun1_N(){</div><div><span style="white-space:pre-wrap"> </span>import std.array;</div><div><span style="white-space:pre-wrap"> </span>//would return Error: gc_malloc cannot be interpreted at compile time, because it has no available source code due to a bug</div>
<div><span style="white-space:pre-wrap"> </span>return [1].array;</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>int fun2(int x){</div>
<div><span style="white-space:pre-wrap"> </span>return 1;</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>auto fun2_N(int x){</div>
<div><span style="white-space:pre-wrap"> </span>import std.array;</div><div><span style="white-space:pre-wrap"> </span>//same as fun1_N</div><div><span style="white-space:pre-wrap"> </span>return [1].array;</div>
<div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>int a1;</div><div><span style="white-space:pre-wrap"> </span>enum a2=0;</div>
<div><br></div><div><span style="white-space:pre-wrap"> </span>static assert(!isCallableCTFE!(()=>a1));</div><div><span style="white-space:pre-wrap"> </span>static assert(isCallableCTFE!(()=>a2));</div>
<div><br></div><div><span style="white-space:pre-wrap"> </span>static assert(isCallableCTFE!fun1);</div><div><span style="white-space:pre-wrap"> </span>static assert(!isCallableCTFE!fun1_N);</div>
<div><br></div><div><span style="white-space:pre-wrap"> </span>static assert(isCallableCTFE!(()=>fun2(0)));</div><div><span style="white-space:pre-wrap"> </span>static assert(!isCallableCTFE!(()=>fun2_N(0)));</div>
<div><span style="white-space:pre-wrap"> </span>//NOTE:an alternate syntax which could be implemented would be: static assert(!isCallableCTFE!(fun2_N,0)));</div><div>}</div>
</blockquote></div><br><div><br></div><div>can we add this to std.traits?</div><div>it allows (among other things) to write unittests for CTFE, etc.</div>