DIP69 - Implement scope for escape proof references

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 8 13:15:48 PST 2014


On 12/7/2014 2:46 AM, Sebastiaan Koppe wrote:
> On Friday, 5 December 2014 at 23:58:41 UTC, Walter Bright wrote:
>> On 12/5/2014 8:48 AM, "Marc Schütz" <schuetzm at gmx.net>" wrote:
>>>     scope ref int foo();
>>>     scope ref int bar1(ref int a) {
>>>         return a;
>>>     }
>>>     scope ref int bar2(scope ref int a) {
>>>         return a;
>>>     }
>>>     ref int bar3(ref int a) {
>>>         return a;
>>>     }
>>>     ref int bar4(scope ref int a) {
>>>         return a;
>>>     }
>>>     void baz(scope ref int a);
>>>
>>> Which of the following calls would work?
>>>
>>>     foo().bar1().baz();
>>
>> yes
>>
>>>     foo().bar2().baz();
>>
>> no - cannot return scope ref parameter
>>
>>>     foo().bar3().baz();
>>
>> yes
>>
>>>     foo().bar4().baz();
>>
>> no, cannot return scope ref parameter
>
> I understand that scope will not allow the contents of the variable to escape
> the lifetime of a declaration. But can you explain why bar1() works, but bar2()
> doesn't?

A 'scope ref' parameter may not be returned as a 'ref' or a 'scope ref'.

> Isn't the body of bar2() in the line `foo().bar2();` part of the
> declaration?
>
> Besides, what does it mean to return a `scope ref int`? Does it mean that the
> content of the variable that is returned is not allowed to escape the scope of
> the calling site? Huh?

It means the reference itself (the pointer) does not escape.



More information about the Digitalmars-d mailing list