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..
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.
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.
It's the docs that are outdated, changing title and will make a pull shortly.
Commit pushed to master at https://github.com/D-Programming-Language/d-programming-language.org https://github.com/D-Programming-Language/d-programming-language.org/commit/53476f012be1641d5df7ab35d7420bad065ec00a Merge pull request #109 from AndrejMitrovic/master Fix issue 4556