Thread overview
[Issue 1759] New: Closures and With Statements
Jan 01, 2008
d-bugmail
Aug 08, 2012
Salih Dincer
Aug 08, 2012
timon.gehr@gmx.ch
Aug 15, 2012
Don
January 01, 2008
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
}


-- 

August 08, 2012
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


--- Comment #1 from Salih Dincer <salihdb@hotmail.com> 2012-08-07 20:56:08 PDT ---
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: -------
August 08, 2012
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                     |


--- Comment #2 from timon.gehr@gmx.ch 2012-08-08 02:05:30 PDT ---
(In reply to comment #1)
> 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: -------
August 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=1759


Don <clugdbug@yahoo.com.au> changed:

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


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2012-08-15 06:36:20 PDT ---
(In reply to comment #1)
> 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: -------