DIP69 - Implement scope for escape proof references
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Thu Dec 4 12:58:56 PST 2014
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.
> 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.
> 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.
> I'm not sure what the difference between scope ref and scope is. is d defined as
> a reference to b, or is d defined as a new variable that is initialized to what
> b points at (a la C++ &) ?
Scope refers to the payload. A scope ref is applying scope to the implied ref
pointer. ref works like C++ &.
More information about the Digitalmars-d
mailing list