www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static inner functions

reply bearophile <bearophileHUGS lycos.com> writes:
A little D2 program:

void main() {
           pure nothrow int foo1(immutable int x) { return x; }
    static pure nothrow int foo2(immutable int x) { return x; }
}


This is the asm of the two inner functions:

_D6test4mainFZv4foo1MFNaNbyiZi    comdat
        enter   4,0
        mov EAX,8[EBP]
        leave
        ret 4

_D6test4mainFZv4foo2FNaNbyiZi comdat
        enter   4,0
        leave
        ret

Is this a compiler mistake? Aren't strongly pure inner function static too?

Bye,
bearophile
Dec 22 2010
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, December 22, 2010 10:17:11 bearophile wrote:
 A little D2 program:
 
 void main() {
            pure nothrow int foo1(immutable int x) { return x; }
     static pure nothrow int foo2(immutable int x) { return x; }
 }
 
 
 This is the asm of the two inner functions:
 
 _D6test4mainFZv4foo1MFNaNbyiZi    comdat
         enter   4,0
         mov EAX,8[EBP]
         leave
         ret 4
 
 _D6test4mainFZv4foo2FNaNbyiZi comdat
         enter   4,0
         leave
         ret
 
 Is this a compiler mistake? Aren't strongly pure inner function static too?
Even if they are conceptually static (and I'm not sure if they are), the compiler would have to recognize them as being static, which it obviously doesn't. Presumably only considers a function static if it's marked that way. But arguably, it's _not_ the same because when dealing with a static inner function, you're essentially dealing with a function pointer whereas when dealing with a non-static inner function, you're essentially dealing with a delegate. Now, assuming that it really doesn't make sense for a delegate to be pure (presumably with the idea that accessing its outer scope would be impure), then I would think that it would make more sense to disallow pure delegates and pure non-static inner functions than to just make them static. But I'm not sure what all the implications of combining purity and delegates are, so without studying it a fair bit more, I'm not quite sure whether it really makes sense to have pure delegates. - Jonathan M Davis
Dec 22 2010
prev sibling parent reply Simon <s.d.hammett gmail.com> writes:
On 22/12/2010 18:17, bearophile wrote:
 A little D2 program:

 void main() {
             pure nothrow int foo1(immutable int x) { return x; }
      static pure nothrow int foo2(immutable int x) { return x; }
 }


 This is the asm of the two inner functions:

 _D6test4mainFZv4foo1MFNaNbyiZi    comdat
          enter   4,0
          mov EAX,8[EBP]
          leave
          ret 4

 _D6test4mainFZv4foo2FNaNbyiZi comdat
          enter   4,0
          leave
          ret

 Is this a compiler mistake? Aren't strongly pure inner function static too?

 Bye,
 bearophile
Why would they be? Surely a pure inner function could access immutable vars from the outer function and still be pure. That's being said, you'd hope the compiler would recognise that foo1 is essentially static and optimise it properly. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Dec 22 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Simon:

 Surely a pure inner function could access immutable vars from the outer 
 function and still be pure.
You are right, thank you (and thank to Jonathan M. D.). Bye, bearophile
Dec 22 2010