www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1759] New: Closures and With Statements

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1759

           Summary: Closures and With Statements
           Product: D
           Version: 2.009
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P3
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: xnknet gmail.com


The following code doesn't crash. However, it does print an unexpected value.
It should print 90, but instead it prints 4202678.

The bug can be worked around by referencing 'obj' directly.

struct SS{ int a, b, c; }
int delegate() addss(SS obj){
        with(obj) return {
                return a+b+c;
        };
}

void main(){
        SS obj = {15, 30, 45};
        auto l = addss(obj);
        writeln(l()); // Prints unexpected value
}


-- 
Jan 01 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1759


Salih Dincer <salihdb hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |salihdb hotmail.com
         Resolution|                            |INVALID



---
This is not a bug it's a feature,
there's solution:

void main() {
  struct SS {
   int a, b, c;
  }
  int delegate () addss(SS obj){
    return {
      with(obj) return a+b+c;
    };
  }
  addss(SS(7, 10, 60))().writeln("(total numbers 77)");
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1759


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |timon.gehr gmx.ch
         Resolution|INVALID                     |




 This is not a bug it's a feature,
This can lead to memory corruption in safe code, therefore it is a bug.
 there's solution:
 
This is a workaround. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 08 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1759


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au




 This is not a bug it's a feature,
No, it's a wrong-code bug. This one has been on my to-do list for years now. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 15 2012