DIP69 - Implement scope for escape proof references

via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 12 03:53:59 PST 2014


On Friday, 12 December 2014 at 00:13:10 UTC, deadalnix wrote:
> On Thursday, 11 December 2014 at 13:55:55 UTC, Marc Schütz 
> wrote:
>> This is a point that most people don't seem to understand yet, 
>> and which wasn't obvious for me either, at the beginning:
>>
>> * A `ref` parameter means that it cannot escape the function, 
>> _except_ by return.
>> * A `scope ref` parameter means that it cannot escape the 
>> function  _ever_, not even by return.
>> * A `scope ref` return means that it cannot leave the current 
>> statement.
>>
>> Therefore, a `scope ref` return value can be passed on to the 
>> next function as a `ref` argument. If that function again 
>> returns a reference (even if not explicitly designated as 
>> `scope`), the compiler will treat it as if it were `scope ref`.
>>
>
> No, it understood.

Steven hadn't, evidently.

> It is simply not useful.

I'm not convinced either.

>
>> I agree, this is important. In my proposal, this works without 
>> transitivity. The wrapper stores the pointer as a `scope` 
>> member, then by copying the wrapper, the pointer gets copied 
>> implicitly, to which the normal scope restrictions apply 
>> (`scope` on members means "will not outlive the aggregate"). 
>> If it stored it as normal non-scope pointer, it couldn't get 
>> assigned in the first place.
>
> Wut ? You cante store anything with grear lifetime, including 
> non scope things (as they'll have infinite lifetime). Meaning 
> the only thing you know, is that thing are possibly scoped.
>
> Meaning you have to assume infinite lifetime with every 
> indirection, which make this proposal useless.

My comments above don't refer to this proposal, but my original 
one:

     struct Wrapper(T) {
         scope T payload;
         // used to be:
         // scope!this T payload;
     }

     scope int a;
     auto w = Wrapper!(int*)(&a); // ok
     scope int b;
     w.payload = &b;              // error, w (and therefore 
w.payload)
                                  // lives longer than b
     Wrapper!(int*) w2 = w;       // ok, lifetime(w2) < lifetime(w)
     w = w2;                      // error, equivalent to
                                  // w.payload = w2.payload

Therefore, wrapping is safe without transitivity of scope.


More information about the Digitalmars-d mailing list