DIP69 - Implement scope for escape proof references

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 4 15:17:16 PST 2014


On 12/4/2014 1:10 PM, Walter Bright wrote:
> On 12/4/2014 11:41 AM, H. S. Teoh via Digitalmars-d wrote:
>> 3) What does scope mean for delegate parameters? To what does the scope
>> apply, the delegate itself, its body, or its return value, or ...?
>>
>>     struct S {
>>         void opApply(scope void delegate(ref int) loopBody) {
>>             ...
>>             // what restrictions (might) apply here w.r.t.
>>             // how loopBody can be called?
>>         }
>>     }
>
> Hmmm, looks like a problem I didn't think of. darnit!

Turns out, 'ref' has exactly the same issue. The resolution is the same:

   alias ref int delegate() dg_t; // ref applies to return type
   void foo(dg_t dg) {
     static int i;
     ref int bar() { return i; } // ref applies to return type
     dg = &bar;
     dg() = 3;
   }

versus:

   void foo(ref int deletate() dg) { // ref applies to declaration dg
     int bar() { return 3; }
     dg = &bar;
   }

Replace 'ref' with 'scope'.


More information about the Digitalmars-d mailing list