DIP69 - Implement scope for escape proof references

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 8 07:12:51 PST 2014


On 12/5/14 6:09 PM, Walter Bright wrote:
> 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.

But you're the programmer that did it, it's YOUR example! :)

However, it's not just that, this is the ONLY example given as to why we 
support scope returns. The language allows it, therefore we must support 
it? But it's not allowed now, right? Why add it?

>>> 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.

I think you should eliminate scope returns then. They are not useful. I 
can't think of a single reason why a newly allocated via GC or global 
reference return should have to be restricted to exist only within the 
statement. Both have infinite lifetimes.

-Steve


More information about the Digitalmars-d mailing list