+1 vote
in D Programming by
Why Can't Nested Functions Be Forward Referenced?

1 Answer

0 votes
by
Declarations within a function are different from declarations at module scope. Within a function, initializers for variable declarations are guaranteed to run in sequential order. Allowing arbitrary forward references to nested functions would break this, because nested functions can reference any variables declared above them.

int first() { return second(); }

int x = first();   // x depends on y, which hasn't been declared yet.

int y = x + 1;

int second() { return y; }

But, forward references of nested functions are sometimes required (eg, for mutually recursive nested functions). The most general solution is to declare a local, nested struct. Any member functions (and variables!) of that struct have non-sequential semantics, so they can forward reference each other.

Related questions

0 votes
asked Apr 17, 2022 in Apache Drill by sharadyadav1986
+1 vote
asked Oct 27, 2020 in JAVA by sharadyadav1986
...