Bug 4556 – Wrong docs for nested functions

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-08-01T14:12:00Z
Last change time
2012-04-19T17:57:39Z
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2010-08-01T14:12:59Z
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 by andrej.mitrovich — 2010-08-01T14:46:50Z
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 by andrej.mitrovich — 2011-05-19T22:00:38Z
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 by andrej.mitrovich — 2012-01-04T07:47:14Z
It's the docs that are outdated, changing title and will make a pull shortly.
Comment #4 by github-bugzilla — 2012-04-14T22:41:35Z