DIP69 - Implement scope for escape proof references

Sebastiaan Koppe via Digitalmars-d digitalmars-d at puremagic.com
Sun Dec 7 02:46:09 PST 2014


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? 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 seemed so easy when you gave the example.

On Friday, 5 December 2014 at 20:55:55 UTC, Walter Bright wrote:
> It means that this code will be safe:
>
>    void foo(scope int* p);
>
>    p = malloc(n);
>    foo(p);
>    free(p);
>
> The rest is all the nuts and bolts of making that work.


More information about the Digitalmars-d mailing list