<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"><<a href="mailto:jmdavisProg@gmx.com">jmdavisProg@gmx.com</a>></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>
> So I wrote a program, to find prime numbers, just to check out this pure<br>
> thing;<br>
><br>
> <a href="http://gist.github.com/608493" target="_blank">http://gist.github.com/608493</a><br>
><br>
> However, the program has a runtime of about 5 seconds? - in my mind, if the<br>
> function is pure, shouldn't the compiler insure that it was evaluated at<br>
> compiletime? - or am I doing it wrong?<br>
<br>
</div>pure != CTFE<br>
<br>
If a function is pure, then it's result can be cached if it'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's a different argument). It's an<br>
optimization that the compiler may or may not choose to do. I don'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'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'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't then that line won't compile).<br>
<br>
Regardless, purity has _nothing_ to do with CTFE (compile-time function<br>
evaluation). It doesn't even necessarily make it easier for the compiler to run<br>
the function at compile time if it'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'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't use delete. It'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'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 'Skeen' Madsen<br>