Escape this in pure members

Per Nordlöw per.nordlow at gmail.com
Sat Sep 19 19:50:09 UTC 2020


On Saturday, 19 September 2020 at 18:48:31 UTC, Jacob Carlborg 
wrote:
> A nested class seems to be able to escape the `this` reference:

Ahh, thanks.

I just realized that it can escape into other parameters without 
the `scope` qualifier?

This

class Bar
{
     void bar(scope Bar b) @safe pure
     {
         b = this;
     }
}

compiles but this

class Bar
{
     scope void bar(scope Bar b) @safe pure
     {
         b = this; // Error: scope variable `this` assigned to `b` 
with longer lifetime
     }
}

fails as

foo.d(6,11): Error: scope variable `this` assigned to `b` with 
longer lifetime


More information about the Digitalmars-d-learn mailing list