<div>Okay I had a misunderstanding of pure, now I get it, thanks :)<br></div><div><br></div><div>and when I changed the code to enum int, the compiler complained about the delete statement too :)</div><div>so once again thanks :)</div>
<br><div class="gmail_quote">On 3 October 2010 13:35, Jonathan M Davis <span dir="ltr">&lt;<a href="mailto:jmdavisProg@gmx.com">jmdavisProg@gmx.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Sunday 03 October 2010 03:54:06 Emil Madsen wrote:<br>
&gt; So I wrote a program, to find prime numbers, just to check out this pure<br>
&gt; thing;<br>
&gt;<br>
&gt; <a href="http://gist.github.com/608493" target="_blank">http://gist.github.com/608493</a><br>
&gt;<br>
&gt; However, the program has a runtime of about 5 seconds? - in my mind, if the<br>
&gt; function is pure, shouldn&#39;t the compiler insure that it was evaluated at<br>
&gt; compiletime? - or am I doing it wrong?<br>
<br>
</div>pure != CTFE<br>
<br>
If a function is pure, then it&#39;s result can be cached if it&#39;s called again with<br>
the same arguments. So, if you hade a pure sqrt() in an expression like so<br>
<br>
auto x = sqrt(2) + sqrt(2) + sqrt(4);<br>
<br>
then the compiler could choose to do the sqrt(2) call only once and use the<br>
result of the first call again instead of calling it again (though it would still<br>
have to call do the sqrt(4) call since it&#39;s a different argument). It&#39;s an<br>
optimization that the compiler may or may not choose to do. I don&#39;t know when<br>
the compiler currently chooses to do it. But regardless, the function is _not_<br>
called at compile time. What if it wasn&#39;t 2 and 4 but rather function arguments?<br>
<br>
void myfunc(int x, int y)<br>
{<br>
        //...<br>
        auto x = sqrt(x) + sqrt(x) + sqrt(y);<br>
        //...<br>
}<br>
<br>
The compiler could cache the result of sqrt(x), but it can&#39;t do it at compile<br>
time. If you want a function to be called at compile time, it needs to be<br>
forced. That is, you have to assign the result to a constant. So,<br>
<br>
enum x = sqrt(x) + sqrt(x) + sqrt(y);<br>
<br>
would be done at compile time - assuming that x and y can be known at compile<br>
time (if they can&#39;t then that line won&#39;t compile).<br>
<br>
Regardless, purity has _nothing_ to do with CTFE (compile-time function<br>
evaluation). It doesn&#39;t even necessarily make it easier for the compiler to run<br>
the function at compile time if it&#39;s pure.<br>
<br>
Now, for your program, assuming that your calcPrimes() function is CTFE-able, if<br>
you change immutable int to enum or enum int, then it will be run at compile<br>
time and your program will be a lot faster than 5 seconds because primes will be<br>
a constant. Of course, if calcPrimes() is _not_ CTFE-able, then you&#39;re out of<br>
lock unless you manage to alter it so that it _is_ CTFE-able.<br>
<br>
Oh, and by the way, don&#39;t use delete. It&#39;s going to be removed from the language<br>
and is completely unnecessary. D is garbage collected. If you want to avoid the<br>
garbage collector, you&#39;re going to need to use malloc() and free()  (which<br>
definitely are _not_ CTFE-able).<br>
<font color="#888888"><br>
- Jonathan M Davis<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>// Yours sincerely<br>// Emil &#39;Skeen&#39; Madsen<br>