Implementing Pure Functions
Timon Gehr
timon.gehr at gmx.ch
Fri Jun 17 08:50:52 PDT 2011
KennyTM~ wrote:
> On Jun 17, 11 22:41, Timon Gehr wrote:
> [snip]
>>
>> A small problem I can see with the purity validation algorithm proposed by > Andrei
>> relates to incremental builds (again):
>>
>> module A;
>> int iampure(){
>> return 5;
>> }
>>
>> module B;
>> int puretemplate()(){ // inferred pure
>> return iampure();
>> }
>>
>> int main(){
>> import std.stdio;
>> writeln(puretemplate(), puretemplate()); // compiler caches result
>> }
>>
>> $ dmd -c A
>> $ dmd -c B
>> $ dmd A.o B.o; ./A
>> 55
>>
>> Some funny guy decides to change A.iampure:
>> module A;
>> int iampure(){
>> static int x;
>> return ++x;
>> }
>>
>> $ dmd -c A
>> $ dmd A.o B.o; ./A
>> 11
>>
>> Wrong! Should be 12.
>
> That happens too if 'iampure' is a template or the compiler inline a
> function which should be modified, with or without caching. I don't see
> this as a problem of attribute inference.
It does not happen if 'iampure' is a template. You get the behavior the template
provided before the change (something you understand quickly), not some strange
mix based on 'wrong' assumptions of the compiler while performing *implicit*
purity inference (which can be hard to track down).
There is also no problem with inlining unless you pass -inline when performing
incremental builds.
Timon
More information about the Digitalmars-d
mailing list