Purity

Bruno Medeiros brunodomedeiros+spam at com.gmail
Thu Jan 27 11:02:55 PST 2011


On 17/12/2010 14:39, Don wrote:
> <1>
>> Another inaccuracy (really an omission) is that a weakly pure function
>> is just like a pure function but cannot be memoized.  In fact, it
>> cannot be optimized in any way like strongly pure functions can.
>
> <2>
>> This actually ties together nicely with my first point -- a pure
>> function that returns a mutable pointer must be weakly pure.
>
> A function which has immutably pure parameters can undergo *some*
> optimisation, even if the return value is a mutable pointer.
> For example, if the parameters are identical for both calls, you can do
> a deepdup of the first return value instead of calling the function again.


Are you sure? If I understood you correctly, that doesn't seem to be the 
case. Consider for example:

string[] func(string arg) pure {
     string elem2 = "blah".idup;
     return [ arg, elem2 ];
}

The compiler *cannot* know (well, looking at the signature only of 
course) how to properly deepdup the result from the first return value, 
so as to give the exact same result as if func was called again.


Apologies if this has been discussed in some thread further ahead.

-- 
Bruno Medeiros - Software Engineer


More information about the Digitalmars-d mailing list