<br><br><div class="gmail_quote">On Tue, Aug 3, 2010 at 9:34 PM, Philippe Sigaud <span dir="ltr"><<a href="mailto:philippe.sigaud@gmail.com">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 class="im">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 class="im">
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 class="im">
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 class="im">
<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><br>The { comes after "typeof(" as in your second example, and then it compiles.<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></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 class="im"><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><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>