March 04, 2015
Template mixin scope seems to have a weird behavior:
I don't understand 'WEIRD(1)' and 'WEIRD(2)' below.

import std.stdio;
struct A{
  int b;
  this(int b){
    this.b=b;
    writeln("A.begin");
  }
  ~this(){
    writeln("A.end");
  }
}

mixin template Entry(){
  auto a=A(12);
}

void test1(){
  writeln("test.begin");
  mixin Entry;
  writeln(a.b);//WEIRD! destructor has been called but a.b is still alive
  writeln("test.end");
}

void test2(){
  writeln("test.begin");
  auto a=A(0);
  writeln("test.end");
}

void test3(){
  writeln("test.begin");
  mixin("auto a=A(0);");
  writeln("test.end");
}

void main(){
  test1;
  writeln;
  test2;
  writeln;
  test3;
}

output:
----------------
test.begin
A.begin
A.end //WEIRD(1): why is destructor called before test.end?
12 //WEIRD(2): destructor has been called but a.b is still alive
test.end

test.begin
A.begin
test.end
A.end

test.begin
A.begin
test.end
A.end
----------------