Why is dtor called for lazy parameter?

Andrey Zherikov andrey.zherikov at gmail.com
Fri Sep 18 10:43:47 UTC 2020


I have this code:
==========
class S
{
     int i = -1;

     this(int n) { i = n; writeln(i," ",__PRETTY_FUNCTION__); }
     ~this() { writeln(i," ",__PRETTY_FUNCTION__); }
}


auto create()
{
     writeln("-> ",__PRETTY_FUNCTION__);
     scope(exit) writeln("<- ",__PRETTY_FUNCTION__);

     return scoped!S(1);
}

auto do_something(S s)
{
     writeln("-> ",s.i," ",__PRETTY_FUNCTION__);
     scope(exit) writeln("<- ",s.i," ",__PRETTY_FUNCTION__);

     return s;
}

auto do_lazy(lazy S s)
{
     writeln("-> ",__PRETTY_FUNCTION__);
     scope(exit) writeln("<- ",__PRETTY_FUNCTION__);

     return s;
}

void main()
{
     writeln("-> ",__PRETTY_FUNCTION__);
     scope(exit) writeln("<- ",__PRETTY_FUNCTION__);

     create()
     .do_lazy()
     .do_something();
}
==========
This is the output:
==========
-> void test.main()
-> test.do_lazy(lazy S s)
-> test.create()
1 S test.S.this(int n)
<- test.create()
1 void test.S.~this()                (1)
<- test.do_lazy(lazy S s)
-> 1703096 test.do_something(S s)    (2)
<- 1703104 test.do_something(S s)    (3)
<- void test.main()
==========

Why is dtor called before returning from do_lazy function - see 
(1)? It seems cause uninitialized parameter in do_something call 
after it in (2)-(3).


More information about the Digitalmars-d-learn mailing list