August 25, 2005
The following code produces errors:
<code>
void main()
{
   test();
}

void test()
{
	int x = 10;
	bool y = testX();
	printf("x = %d, y = %d", x, cast(int)y);

	bool testX()
	{
	   return x == 5;
	}
}
</code>

Output:
-----------
>dmd nested
nested.d(9): undefined identifier testX
nested.d(9): function expected before (), not testX of type int
nested.d(9): cannot implicitly convert expression (testX()) of type int to bit
-----------

A nested function is being called lexically prior its definition, the compiler complains that it's undefined.

Not only that, but for some strange reason, the compiler assumed that this nested function (or its name?) is of type "int"! (try it with a nested function of some class type, and it will also assume it to be of type int)

This seems analogous to C++'s behaviour of not seeing a function who's declaration doesn't lexically appear before the reference.

Is this intended or not?
I would've posted it in the bugs section, but I'm not sure whether or not it's an intended behaviour or a bug.

I couldn't find anything about it in the specs, but here:
http://www.digitalmars.com/d/sdwest/paper.html
<quote>
Symbol table
Symbols are looked up using natural lexical scoping rules. All module level symbols are available, even if they only appear lexically after the reference.
</quote>
and right below it, nested functions are mentioned, but no exception to this rule is introduced.

and:
http://www.digitalmars.com/d/overview.html
"Functions can be defined in a natural order rather than the typical inside-out order commonly used in C programs to avoid writing forward declarations."

And here it talks about nested functons, and again, no exception is mentioned:
http://www.digitalmars.com/d/function.html#nested
The only thing mentioned in this regard is:
"Nested functions can only be accessed by the most nested lexically enclosing function, or by another nested function at the same nesting depth"
It doesn't exclude that part of the enclosing function which appears lexically before the definition of the nested function.

Is this a bug, or am I missing something?

By the way, this compiles:
<code>
void main()
{
   test();
}

void test()
{
	int x = 10;
	bool testX()
	{
	   return x == 5;
	}
	bool y = testX();
	printf("x = %d, y = %d", x, cast(int)y);
}
</code>
August 26, 2005
On Thu, 25 Aug 2005 17:56:01 -0600, Hasan Aljudy wrote:

> The following code produces errors:
> <code>
> void main()
> {
>     test();
> }
> 
> void test()
> {
> 	int x = 10;
> 	bool y = testX();
> 	printf("x = %d, y = %d", x, cast(int)y);
> 
> 	bool testX()
> 	{
> 	   return x == 5;
> 	}
> }
> </code>
> 
> Output:
> -----------
>  >dmd nested
> nested.d(9): undefined identifier testX
> nested.d(9): function expected before (), not testX of type int
> nested.d(9): cannot implicitly convert expression (testX()) of type int
> to bit
> -----------
> 
> A nested function is being called lexically prior its definition, the compiler complains that it's undefined.

There is an oblique reference in the docs about this in the Nested Functions section ...

 --------------------------------------
Unlike module level declarations, declarations within function scope are
processed in order. This means that two nested functions cannot mutually
call each other:

void test()
{
    void foo() { bar(); }// error, bar not defined
    void bar() { foo(); }// ok
}

The solution is to use a delegate:

void test()
{
    void delegate() fp;
    void foo() { fp(); }
    void bar() { foo(); }
    fp = &bar;
}
Future directions: This restriction may be removed
 ---------------------------------------

The 'processed in order' phrase implies that forward referencing of nested functions is not (yet) available to us.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
26/08/2005 10:28:24 AM