Pure not acting pure.

bearophile bearophileHUGS at lycos.com
Thu Jun 16 05:04:22 PDT 2011


Michel Fortin:

> Just make sure all the parameters are either const or immutable or 
> passed by copy and do not contain any pointer or reference. That'll 
> make the function strongly pure, and the compiler will be able optimize.

I have just done a little test on this, currently DMD calls sqr one time in main() only if you add "nothrow" too:

pure nothrow int sqr(int x) { return x * x; }
int main() {
    enum int n = 10;
    immutable int r = sqr(n) + sqr(n);
    return r;
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list