Oh and there's a shorter way to write this example, by using isInputRange from std.range, like so:<br><br>if (isInputRange!R && is(typeof({x = fun(x, range.front);})))<br><br>This was in TDPL (except the {}'s which are missing).<br>
<br><div class="gmail_quote">On Tue, Aug 3, 2010 at 10:01 PM, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com">andrej.mitrovich@gmail.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;">
<br><br><div class="gmail_quote"><div class="im">On Tue, Aug 3, 2010 at 9:34 PM, Philippe Sigaud <span dir="ltr"><<a href="mailto:philippe.sigaud@gmail.com" target="_blank">philippe.sigaud@gmail.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;">
<br><br><div class="gmail_quote"><div>On Tue, Aug 3, 2010 at 20:59, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com" target="_blank">andrej.mitrovich@gmail.com</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div>
There seem to be some bugs with template constraints. Here's a reduce example (from TDPL) which will not compile:<br><br>
<br></div><div>
V reduce(alias fun, V, R)(V x, R range)<br>
if (is(typeof(x = fun(x, range.front)))<br>
&& is(typeof(range.empty) == bool)<br>
&& is(typeof(range.popFront())))<br></div></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;">I'm filing a bug unless something else is to blame here.<br>
</blockquote></div><br><div>I think that's because you cannot directly take the type of a statement. The assignment in the first typeof() is to blame. To make a statement into an expression, transform it into an anonymous void delegate(): put it in braces (with a semicolon at the end) and call it like a function, like this:</div>
<div>
<div><br></div><div>V reduce(alias fun, V, R)(V x, R range)</div><div> if (is({ typeof(x = fun(x, range.front);}()))</div><div> && is(typeof(range.empty) == bool)</div><div> && is(typeof(range.popFront())))</div>
</div><div>{...}</div><div><br></div></blockquote></div><div><br>The { comes after "typeof(" as in your second example, and then it compiles.<br> </div><div class="im"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div></div><div>Or, more readable, wrap all the code you want to test into curly brackets and evaluates its global return type:</div><div><br></div><div><div><div><br></div><div>V reduce(alias fun, V, R)(V x, R range)</div>
</div><div> if (is(typeof({ // I want to be able to do that with an R and a V:</div><div> x = fun(x, range.front);</div><div> if (range.empty) {};</div><div>
range.popFront();</div>
<div> }())))</div><div> </div></div><div>{...}</div><div><br></div><div>It's a D idiom you'll see in many places in the standard library. I personally find it a _bit_ heavy on parenthesis, even though I like Lisp.</div>
<div><br></div><font color="#888888"><div><br></div><div>Philippe</div>
</font></blockquote></div></div><br>Yeah, those paranthesis are getting a bit scary now. :) I guess this one goes to the TDPL errata.<br><br>Thanks for your help Philippe.<br><br><br>
</blockquote></div><br>