Forward reference to nested function not allowed?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 1 21:34:16 PDT 2014


On Sat, 31 May 2014 16:18:33 +0000
DLearner via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> Hi,
>
> import std.stdio;
> void main() {
>
>     writefln("Entered");
>
>     sub1();
>     sub1();
>     sub1();
>
>     writefln("Returning");
>
>     void sub1() {
>        static int i2 = 6;
>
>        i2 = i2 + 1;
>        writefln("%s",i2);
>     };
> }
>
> does not compile, but
>
> import std.stdio;
> void main() {
>     void sub1() {
>        static int i2 = 6;
>
>        i2 = i2 + 1;
>        writefln("%s",i2);
>     };
>     writefln("Entered");
>
>     sub1();
>     sub1();
>     sub1();
>
>     writefln("Returning");
>
>
> }
>
> compiles and runs as expected.
>
> Is this intended?

Currently, you cannot forward reference a nested function. Kenji was looking
into it recently, so maybe we'll be able to at some point in the future, but
right now, we definitely can't. But even if we can in the future, I'd expect
that you'd have to declare a prototype for the function to be able to use it
before it's declared. In general though, I think that the only reason that it
would really be useful would be to have two nested functions refer to each
other, since otherwise, you just declare the nested function earlier in the
function.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list