DIP69 - Implement scope for escape proof references

Sebastiaan Koppe via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 5 20:31:47 PST 2014


On Friday, 5 December 2014 at 20:55:55 UTC, Walter Bright wrote:
> On 12/5/2014 7:27 AM, Steven Schveighoffer wrote:
>> Can someone who knows what this new feature is supposed to do 
>> give some Ali
>> Çehreli-like description on the feature? Basically, let's 
>> strip out the *proof*
>> in the DIP (the how it works and why we have it), and focus on 
>> how it is to be
>> used.
>>
>> I still am having a hard time wrapping my head around the 
>> benefits and when to
>> use scope, scope ref, why I would use it. I'm worried that we 
>> are adding all
>> this complication and it will confuse the shit out of users, 
>> to the point where
>> they won't use it.
>
> The tl;dr version is when a declaration is tagged with 'scope', 
> the contents of that variable will not escape the lifetime of 
> that declaration.
>
> 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.

<brainstorm>
What about also adding the inverse of scope? Then scope can be 
inferred. As in:

```
void foo(int* p);
void free(P)(consume P* p);

p = malloc(n);
foo(p);  // here foo() gets scope, since free consumes it.
free(p);
```

So you do not need to write scope everywhere. And it would 
prevent this code:

```
{
   free(p);
   free(p);  // Error: p already consumed
}
```
</brainstorm>


More information about the Digitalmars-d mailing list