On Friday, 18 September 2020 at 10:43:47 UTC, Andrey Zherikov
wrote:
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).
As ikod mentions, it's because of scoped!S. As for why it does
this, yeah, this is kinda egregious. The documentation[0] of
scoped mentions this issue:
This facility is unsafe; it is the responsibility of the user
to not escape a reference to the object outside the scope.
As the name implies, scoped!T limits the valid scope of a
reference to the scope of the variable holding it. You're putting
it on the stack, so the moment the variable goes off the stack
(when do_lazy returns), the reference is destructed.
--
Simen
[0]: https://dlang.org/library/std/typecons/scoped.html