Escape this in pure members
DlangUser38
DlangUser38 at nowhere.se
Wed Sep 23 00:06:38 UTC 2020
On Tuesday, 22 September 2020 at 18:21:10 UTC, Jacob Carlborg
wrote:
> 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`?
The following analysis might be wrong but I think that `scope` as
a **member** function attribute is not supposed to be used as
that is not even documented.
So it would works "by default". The compiler thinks that `this`
is a scope variable that will stop living after `bar()`.
Also as `b` is not `ref` this is clearly a wrong diagnostic.
There's a special case missing in the compiler.
More information about the Digitalmars-d-learn
mailing list