Escape this in pure members

Jacob Carlborg doob at me.com
Tue Sep 22 18:21:10 UTC 2020


On 2020-09-19 21:50, Per Nordlöw wrote:
> 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
>      }
> }

Hmm, why would `b` have longer lifetime? Isn't the lifetime of `b` 
throughout `bar`?

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list