Forward declaration inside Function block, no error?

Rubn where at is.this
Sun Jan 6 20:19:59 UTC 2019


On Sunday, 6 January 2019 at 18:38:44 UTC, Benjamin Thaut wrote:
> Today I found a bug in my D code.
>
>
> import std.stdio;
>
> // Type your code here, or load an example.
> void grow()
> {
>     writeln("grow");
> }
>
> void someFunc(bool condition)
> {
>     if(condition)
>     {
>         void grow();
>     }
> }
>
>
> I tried to call the grow function, but accidentially copied the 
> return value alongside the function name. I was wondering why 
> this code compiles without errors. the "void grow();" becomes a 
> no-op. In my opinion this could should not compile. Am I 
> missing something here?
>
> Kind Regards
> Benjamin Thaut

import std.stdio;

void grow()
{
     writeln("grow");
}

void someFunc(bool condition)
{
     if(condition)
     {
         void grow();
         pragma(msg, grow.mangleof); // _D3app8someFuncFbZ4growMFZv
     }
}

You can declare functions inside of functions in D. You weren't 
forward declare grow() in the module namespace, so much as you 
were forward declaring a new function grow.



More information about the Digitalmars-d-learn mailing list