[Dlang-study] [lifetime] Initial thoughts on lifetime management

Marc Schütz schuetzm at gmx.net
Wed Oct 28 08:15:23 PDT 2015


On Wednesday, 28 October 2015 at 00:46:34 UTC, Andrei 
Alexandrescu wrote:
> I should add here another pattern that turned problematic for 
> our older attempts in DIP74:
>
> C c = new C();
> foo(c);
>
> int foo(scope C d) {
>      c = new C();    // c's old instance gets deleted
>      return d.i;        // oops! d is invalid
> }
>

That's because global variables have potentially shared 
ownership. They need special treatment (for example, in Rust many 
things you can do with them require unsafe blocks). If `c` were 
an argument to `foo`, the compiler could catch the problem at the 
call site.


More information about the Dlang-study mailing list