bug or not? with(mystruct()) { code; } calls struct destructor before code

J not_avail at notavailable.com
Sat Mar 23 04:23:19 PDT 2013


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";
     }
}


More information about the Digitalmars-d-learn mailing list