<div class="gmail_quote">2010/8/30 Andrei Alexandrescu <span dir="ltr"><<a href="mailto:SeeWebsiteForEmail@erdani.org">SeeWebsiteForEmail@erdani.org</a>></span><br><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><div class="h5">On 8/30/10 14:16 PDT, Tomek Sowiński wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Dnia 30-08-2010 o 21:10:10 Tomek Sowiński <<a href="mailto:just@ask.me" target="_blank">just@ask.me</a>> napisał(a):<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
How about reduce!fun(range)? It's pure/nothrow when fun is<br>
pure/nothrow. Plenty of std.algorithm would benefit.<br>
</blockquote>
<br>
Eh, nevermind. popFront() must mutate the range so it can't be pure.<br>
Need to get some sleep...<br>
</blockquote>
<br></div></div>
It can still be nothrow depending on input, which makes a solid point. Arguments against qualifier/attribute propagation based on sheer semantics ("it's abs so it must be pure") break badly in the face of higher-order functions.<br>
<br>
It's pretty clear we need an attribute propagation mechanism if we want e.g. to make Phobos const-aware.<font color="#888888"></font></blockquote><div><br>Could something like this be a possible building block for pure? <br>
<br>import std.stdio, std.bigint;<br><br>/**<br>Test whether expression expr is pure for type Type.<br>*/<br>template isPure(Type, string expr)<br>{<br> enum isPure = isPureImpl!(Type, expr);<br>}<br><br>bool isPureImpl(Type, string expr)()<br>
{<br> mixin("auto pureTester(T)(T a) pure { return " ~ expr ~ ";}");<br> return is(typeof( pureTester(Type.init) ));<br>}<br><br>int foo(int i) { return i;} // not pure<br>int bar(int i) pure { return i;}// pure<br>
<br>void test(T)(T num) if (isPure!(T, "-1*a")) // test that with "-1*a", "-1*foo(a)" and "-1*bar(a)"<br>{<br> writeln("Yes, pure expr for type " ~ T.stringof);<br>
}<br><br>void main()<br>{<br> test(1);<br> auto b = BigInt("1");<br>// test(b); // is -1*b a pure expr for BigInts?<br>}<br> <br>test(1) will compile for expression -1*a or just "a" <br>test(BigInt("1")) will not compile with expression -1*a, because it's not pure for BigInts. It will compile with just "a" as constraint expression.<br>
<br>I also tried to instantiate test(1) with -1*bar(a) and -1*foo(a) as constraints. It passes for -1*bar(a), because bar is pure, but not with foo(a).<br><br>Maybe then it's possible to create a pure version of a function if this passes the test. I'm not sure, it's late in there. <br>
Also, this could be done also for nothrow. I didn't think it through for const.<br><br>Philippe<br></div></div>