DIP69 - Implement scope for escape proof references

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 4 12:41:58 PST 2014


On 12/4/2014 4:03 AM, Martin Nowak wrote:
> On Thursday, 4 December 2014 at 09:25:11 UTC, Walter Bright wrote:
>> http://wiki.dlang.org/DIP69
>>
> Great stuff.
>
> Will this be possible, it's a fairly important use-case.
>
> scope ref T setVal(scope ref T t)
> {
>      t.val = 12;
>      return t;
> }

Yes, it would be written:

   scope ref T setVal(ref T t)
   {
      t.val = 12;
      return t;
   }


> Another question, how would a reference counted pointer take advantage of scope,
> i.e. avoid the increment/decrement when being passed to a function?
> One solution would be to add a function that returns a scoped reference to the
> underlying value.
>      struct RefCounted(T)
>      {
>          scope ref T borrow() { return *p; }
>      }
> Will it be possible to deduce, that the lifetime of that scoped value is tied to
> the smart pointer?

   struct RefCounted(T)
   {
     T t;
     scope ref T borrow() { return t; }
     alias this t;
   }

This enables RefCounted!T to be implicitly converted to a T, but with a scoped 
result. This is a critical feature, one I spent a lot of time thinking about, 
and hope it's right :-)



More information about the Digitalmars-d mailing list