Purity

Simen kjaeraas simen.kjaras at gmail.com
Fri Jan 28 12:25:29 PST 2011


Bruno Medeiros <brunodomedeiros+spam at com.gmail> wrote:

> On 27/01/2011 21:05, Simen kjaeraas wrote:
>> Bruno Medeiros <brunodomedeiros+spam at com.gmail> wrote:
>>
>>> 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.
>>
>> Could you please elucidate, as I am unsure of your reasoning for saying
>> the compiler cannot know how to deepdup the result.
>>
>
>
> string str = "blah";
> string[] var1 = func(str);
> string[] var2 = func(str);
>
> How can the compiler optimize the second call to func, the one that is  
> assigned to var2, such that he deepdups var1 instead of calling func  
> again? Which code would be generated?
>
> The compiler can't do that because of all the transitive data of var1,  
> the compiler doesn't know which of it was newly allocated by func, and  
> which of it was reused from func's parameters or some other global  
> inputs.

But for immutable data (like the contents of the elements of a string[]),
that doesn't matter, does it?

-- 
Simen


More information about the Digitalmars-d mailing list