[SO] Modifing local class instance in pure function
    bearophile 
    bearophileHUGS at lycos.com
       
    Fri Jun 19 11:44:17 PDT 2009
    
    
  
div0:
> Objects are defined to have an identity, so by returning a new object
> from the function you are not getting the same result for the same inputs.
> Does D relax the definition of pure to say a function may return an
> equivalent object?
Interesting question, I have tried the following code with D2:
import std.stdio: writeln;
class C {}
pure C genc() { return new C; }
void main() {
    writeln(genc() is genc());
}
The generated asm shows that pure of genc is ignored:
With "pure":
main:
L0:     push    EBX
        mov EAX,offset FLAT:_D5temp31C7__ClassZ
        push    EAX
        call    near ptr __d_newclass
        add ESP,4
        mov ECX,offset FLAT:_D5temp31C7__ClassZ
        push    EAX
        sub ESP,4
        push    ECX
        call    near ptr __d_newclass
        add ESP,4
        add ESP,4
        mov EDX,EAX
        mov EBX,1
        pop EAX
        cmp EAX,EDX
        je  L32
        xor EBX,EBX
L32:        mov EAX,offset FLAT:_D3std5stdio6stdoutS3std5stdio4File
        push    EBX
        push    0Ah
        call    near ptr _D3std5stdio4File14__T5writeTbTaZ5writeMFbaZv
        xor EAX,EAX
        pop EBX
        ret
        
Without "pure":
main:
L0:     push    EBX
        mov EAX,offset FLAT:_D5temp31C7__ClassZ
        push    EAX
        call    near ptr __d_newclass
        add ESP,4
        mov ECX,offset FLAT:_D5temp31C7__ClassZ
        push    EAX
        sub ESP,4
        push    ECX
        call    near ptr __d_newclass
        add ESP,4
        add ESP,4
        mov EDX,EAX
        mov EBX,1
        pop EAX
        cmp EAX,EDX
        je  L32
        xor EBX,EBX
L32:        mov EAX,offset FLAT:_D3std5stdio6stdoutS3std5stdio4File
        push    EBX
        push    0Ah
        call    near ptr _D3std5stdio4File14__T5writeTbTaZ5writeMFbaZv
        xor EAX,EAX
        pop EBX
        ret        
I don't know what's happening under the hood into dmd here, Don may give a better answer.
Bye,
bearophile
    
    
More information about the Digitalmars-d-learn
mailing list