D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4556 - Wrong docs for nested functions
Summary: Wrong docs for nested functions
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-01 14:12 UTC by Andrej Mitrovic
Modified: 2012-04-19 17:57 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2010-08-01 14:12:59 UTC
This is the 4th example from the title "Nested Classes" on the page http://www.digitalmars.com/d/2.0/class.html :

import std.stdio;

class Base
{
    int foo() { return 1; }
}

Base func()
{   
    int m = 3;

    class Nested : Base
    {
	int foo() { return m; }
    }

    Base b = new Nested;

    assert(b.foo() == 3);	// Ok, func() is still active
    return b;
}

int test()
{
    Base b = func();
    return b.foo();		// Error, func().m is undefined
}

void main()
{
    writeln(test());  // writes 3, there was no error
}


According to the docs, this should not compile. 
This looks to me like some kind of automatic closure where foo() becomes a delegate? I'm not sure..
Comment 1 Andrej Mitrovic 2010-08-01 14:46:50 UTC
See also the 8th example under "Nested Functions" on this page
http://www.digitalmars.com/d/2.0/function.html. Pasted here:

void test()
{   int j;
    static int s;

    struct Foo
    {   int a;

	int bar()
	{   int c = s;		// ok, s is static
	    int d = j;		// error, no access to frame of test()

	    int foo()
	    {
		int e = s;	// ok, s is static
		int f = j;	// error, no access to frame of test()
		return c + a;	// ok, frame of bar() is accessible,
				// so are members of Foo accessible via
				// the 'this' pointer to Foo.bar()
	    }

	    return 0;
	}
    }
}

This compiles without errors.
Comment 2 Andrej Mitrovic 2011-05-19 22:00:38 UTC
This was not a rejects-valid. The compiler doesn't reject, it accepts the two code snippets.

But I think they're both correct. I've seen code similar to this in TDPL. The compiler does all the work of creating delegates when needed, right? So the docs should be updated to reflect that.
Comment 3 Andrej Mitrovic 2012-01-04 07:47:14 UTC
It's the docs that are outdated, changing title and will make a pull shortly.