Static inner functions
Jonathan M Davis
jmdavisProg at gmx.com
Wed Dec 22 11:06:00 PST 2010
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
More information about the Digitalmars-d-learn
mailing list