Why is dtor called for lazy parameter?

Simen Kjærås simen.kjaras at gmail.com
Fri Sep 18 11:31:39 UTC 2020

On Friday, 18 September 2020 at 10:43:47 UTC, Andrey Zherikov 
> 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.


[0]: https://dlang.org/library/std/typecons/scoped.html

More information about the Digitalmars-d-learn mailing list