www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bug or not? with(mystruct()) { code; } calls struct destructor before

reply "J" <not_avail notavailable.com> writes:
Is this a bug?  I'm very puzzled; it seems that the destructor 
for struct mystruct is called pre-maturely. It should be after 
the 'in call(): X 12, Y 0, Z 9" line, should it not?

Using DMD64 D Compiler v2.062 on OSX 10.6.8 and Linux x86_64

Output is:

$ dmd -run named14.d
start of main
in mystruct dtor: X 0, Y 0, Z 0
top of with block
in call(): X 12, Y 0, Z 9
end of main

//code:
import std.stdio;

void main(string[] arg) {
   writeln("start of main ");
   with(mystruct()) {
        writeln("top of with block");
        x=12;
        z=9;
        call();
   }
   writeln("end of main ");
}

struct mystruct
{
    int x, y, z;
    ~this() {
       writefln("in mystruct dtor: X %s, Y %s, Z %s", x, y, z );
     }

     string call() {
       writefln("in call(): X %s, Y %s, Z %s", x, y, z );
       return "yo";
     }
}
Mar 23 2013
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/23/13, J <not_avail notavailable.com> wrote:
 Is this a bug?
It's a known bug: http://d.puremagic.com/issues/show_bug.cgi?id=8269
Mar 23 2013