Forward declaration issue

Andre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 4 03:32:01 PST 2015


On Friday, 4 December 2015 at 09:51:30 UTC, Artur Skawina wrote:

> No, it's how D is designed -- inside functions the order of 
> declarations matters (and forward declarations don't work).
>
> Your version wrongly declares another `baz` at module scope, 
> and, as there's no definition, you end up with the linker error.
>
> Two workarounds:
>
> 1) Templatize the functions:
>
>    void foo()
>    {
>       void bar()()
>       {
>           baz();
>       }
>
>       void baz()()
>       {
>           bar();
>       }
>
>       baz();
>    }
>
> 2) Use a struct:
>
>    void foo()
>    {
>       struct Hack {
>          void bar()
>          {
>              baz();
>          }
>
>          void baz()
>          {
>              bar();
>          }
>       }
>
>       Hack hack;
>
>       hack.baz();
>    }
>
> artur

Thanks for the clarifications and the example.

Kind regards
André



More information about the Digitalmars-d-learn mailing list