DIP69 - Implement scope for escape proof references

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 5 15:09:10 PST 2014


On 12/4/2014 1:32 PM, Steven Schveighoffer wrote:
> On 12/4/14 3:58 PM, Walter Bright wrote:
>> On 12/4/2014 7:25 AM, Steven Schveighoffer wrote:
>>> int* bar(scope int*);
>>> scope int* foo();
>>>
>>> bar(foo());           // Ok, lifetime(foo()) > lifetime(bar())
>>>
>>> I'm trying to understand how foo can be implemented in any case. It
>>> has no scope
>>> ints to return, so where does it get the int from?
>>
>> Could be from a global variable. Or a new'd value.
>
> Well, OK, but why do that?

Why would a programmer do that? I often ask that question! But the language 
allows it, therefore we must support it.


>>> I don't see where the proposal defines what exactly can be returned
>>> via scope.
>>
>> The scope return value does not affect what can be returned. It affects
>> how that return value can be used. I.e. the return value cannot be used
>> in such a way that it escapes the lifetime of the expression.
>
> I assumed the scope return was so you could do things like:
>
> scope int *foo(scope int *x)
> {
>     return x;
> }
>
> which would be fine, I assume, right?

No. A scope parameter means the value does not escape the function. That means 
you can't return it.


> My question was about how this kind of allows declaring a ref variable in the
> middle of a function, which was never allowed before.

There's no technical reason it is disallowed - it's just that I didn't see a 
point to it.



More information about the Digitalmars-d mailing list