Thread overview
[Issue 4556] New: Misbehaving nested function
Aug 01, 2010
Andrej Mitrovic
Aug 01, 2010
Andrej Mitrovic
May 20, 2011
Andrej Mitrovic
[Issue 4556] Wrong docs for nested functions
Jan 04, 2012
Andrej Mitrovic
Apr 20, 2012
Andrej Mitrovic
August 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4556

           Summary: Misbehaving nested function
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-01 14:12:59 PDT ---
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..

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4556



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-01 14:46:50 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 20, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4556


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2011-05-19 22:00:38 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4556


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Misbehaving nested function |Wrong docs for nested
                   |                            |functions


--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-01-04 07:47:14 PST ---
It's the docs that are outdated, changing title and will make a pull shortly.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4556



--- Comment #4 from github-bugzilla@puremagic.com 2012-04-14 22:41:35 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4556


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------