<br><br><div class="gmail_quote">On Tue, Aug 3, 2010 at 20:59, 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
There seem to be some bugs with template constraints. Here's a reduce example (from TDPL) which will not compile:<br><br>
<br>
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></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;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><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><br></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><br></div><div>V reduce(alias fun, V, R)(V x, R range)</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><div><br></div><div>Philippe</div>