Purity
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Fri Feb 11 05:44:08 PST 2011
On 01/02/2011 14:15, Simen kjaeraas wrote:
> Bruno Medeiros <brunodomedeiros+spam at com.gmail> wrote:
>
>>> But for immutable data (like the contents of the elements of a
>>> string[]),
>>> that doesn't matter, does it?
>>>
>>
>> Maybe it won't matter for the *contents of the elements* of the string
>> array, but the whole result value has to be /the same/ as if the
>> optimization was not applied. Otherwise the optimization is invalid,
>> even if for most uses of the result value it would not make a
>> difference for the program.
>
> I admit to still not understanding this.
>
> The data can't be changed, so the contents do not matter. The array structs
> (prt/length) would not be the same as those fed to the function in any
> case,
> so I really cannot see how those would matter.
>
> If others do understand, please elucidate.
>
Here is a synthetic example:
string[] func(string arg) pure {
string elem2 = "blah".idup;
return [ arg, elem2 ];
}
string str = "blah";
string[] result1 = func(str);
string[] result2 = func(str);
if(result2[0] is str) {
// then
} else {
// else
}
In this code sample if the optimization is applied on the second call to
func, it would cause different code with be executed: the else clause
instead of the then clause. Obviously this is not acceptable for an
optimization, even if such code would be rare in practice.
--
Bruno Medeiros - Software Engineer
More information about the Digitalmars-d
mailing list