<div class="gmail_quote">On Mon, Sep 6, 2010 at 23:31, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@test.com">andrej.mitrovich@test.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
That still won't work. Observe:</blockquote><div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">template isInputRange(R)<br><div class="im">
{<br>
</div> enum bool isInputRange = isValidCode!(<br>
<div class="im"> {<br>
R r; // can define a range object<br>
if (r.empty) {} // can test for empty<br>
r.popFront; // can invoke next<br>
auto h = r.front; // can get the front of the range<br>
});<br>
}<br>
<br>
</div><div class="im">template isValidCode(alias code) { enum bool isValidCode = __traits(compiles, code); }<br>
<br>
</div>Instead of returning false, it will give out a compiler error.<br></blockquote><div><br>That's because the part between the curly braces is evaluated before being passed to the template. And there is no lazy alias.<br>
As Mafi said, you can use a string, it's still the best way to move code around in D. With q{ ... }, it's palatable.<br>And no, before you try it, there is no way to pass the {...} to another template that would stringify it into a q{...} :-)<br>
<br>Maybe, eventually, something like this:<br><br>import std.stdio;<br><br><br>template isValidCode(alias code)<br>{<br> template For(T)<br> {<br> enum bool For = __traits(compiles, code(T.init));<br> }<br>
}<br><br>void main()<br>{<br> // use an anonymous templated function:<br>alias isValidCode!((r)<br> {<br> if (r.empty) {} // can test for empty<br> r.popFront; // can invoke next<br>
auto h = r.front; // c<br> }<br> ) isInputRange;<br><br>// writeln(isInputRange.For!(int[]));<br>}<br><br>Except DMD doesn't like the commented-out line. Whaoh!<br>
<br><br><br>Philippe<br><br></div></div>