www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics




digitalmars.D.bugs - [Issue 2043] New: Closure outer variables in nested blocks are not allocated/instantiated correctly.

http://d.puremagic.com/issues/show_bug.cgi?id=2043

           Summary: Closure outer variables in nested blocks are not
                    allocated/instantiated correctly.
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: brunodomedeiros+bugz gmail.com


An outer variable declared in a nested (and scoped) block that is captured by a
closure will be allocated on the heap, but only once per function execution,
instead of once per block execution, which is what makes sense.
See code below:

--- ---
void delegate()[] dgList;

void test() {

        foreach(int i; [1, 2, 3]) {
                int b = 2;
                dgList ~= { writefln(b); };
                writefln(&b); // b will be the *same* on each iteration
        }
}
---


-- 
Apr 26 2008