DIP69 - Implement scope for escape proof references
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Thu Dec 4 13:32:48 PST 2014
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?
>> 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?
>> Another thing I saw early on:
>>
>> void abc() {
>> scope int* a;
>> int* b;
>> scope ref int* c = a; // Error, rule 5
>> scope ref int* d = b; // Ok
>> int* i = a; // Ok, scope is inferred for i
>> global_ptr = d; // Error, lifetime(d) < lifetime(global_ptr)
>> global_ptr = i; // Error, lifetime(i) < lifetime(global_ptr)
>> int* j; // Ok, scope is inferred for i
>> global_ptr = j; // Ok, j is not scope
>> }
>>
>> Does this mean ref can now be applied to a variable?
>
> scope ref int p = x;
>
> is the same meaning as:
>
> foo(scope ref int p);
> foo(x);
>
> as far as what scope ref means. I found it convenient to use scope ref
> local variables to describe semantics. Whether we want to actually
> enable their usage hinges on if there's a point to it. Technically, it
> should work.
My question was about how this kind of allows declaring a ref variable
in the middle of a function, which was never allowed before.
-Steve
More information about the Digitalmars-d
mailing list