How Nested Functions Work, part 1

Jeremie Pelletier jeremiep at gmail.com
Mon Aug 31 13:01:52 PDT 2009


Walter Bright Wrote:

> language_fan wrote:
> > For what it's worth, they also seem somewhat buggy in D. Hopefully 
> > writing the articles motivates fixing those issues :) For example one 
> > can't call symbols before their declaration on non-global scope.
> 
> That's actually not a bug. In function scopes, you can't reference a 
> variable before use, either.

You have to agree however that this is counter-intuitive, since functions in D can be used before they are declared, yet nested functions cant.

While it follows the convention used by local variables, this isnt the same, local variables need storage, the nested functions merely need a pointer to the stack frame.

Also, using delegates to dereference a nested function so it can be used as a prototype (which to me feels more like a hack than a feature) forces the entire stack frame to be allocated in _d_allocmemory.

Nested functions should be parsed in the same pass as their parent method, fixing the need to use delegates which also needlessly use 8bytes in the stack frame.



More information about the Digitalmars-d mailing list