The next iteration of scope

via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 16 07:10:52 PDT 2015


On Monday, 16 March 2015 at 12:21:17 UTC, Nick Treleaven wrote:
> On 15/03/2015 19:11, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>> On Sunday, 15 March 2015 at 17:31:17 UTC, Nick Treleaven wrote:
>>> I too want a scope attribute e.g. for safe slicing of static 
>>> arrays,
>>> etc. I'm not sure if it's too late for scope by default 
>>> though, perhaps.
>>
>> If we get @safe by default, we automatically get scope by 
>> default, too.
>
> I don't follow that. In @safe code parameters could still 
> default to escaping, causing compiler errors when passing 
> things that can't be allowed to escape.
>
> I do see that scope parameters by default would probably cause 
> less churn than having to put `scope` on non-template function 
> parameters in order to be usable by non-GC-heap memory.

It's part of the proposal, under "Implicit scope": @safe implies 
scope. I think this would cause the least compatibility problems, 
because what can be done with references in @safe code is already 
restricted. And as @safe is going to be used more and more, scope 
will spread with it.

> We already have scope on local delegates (at least it's 
> accepted by dmd):
>
> Object obj;
> scope del = ()=>obj;

Oh, I thought this was deprecated, together with scope classes. I 
was aware that `scope` already works (at least partially) for 
delegate params, but not for locals. Anyway, I think it's 
unnecessary for locals, because:

>
> Also, what would be the 'type' of a static array slice without 
> `scope` applying to locals?
>
> int[2] arr = [1, 2];
> scope int[] s = arr;
>
> I suppose `scope` can be inferred for s, I'm just pointing out 
> that it can apply to locals.

Yes, it will be inferred. I've removed this part of the proposal 
from the article, because I thought it is too technical, and I 
wanted to describe only what would be visible to the end-user. 
But I moved my notes about that to the talk page [1]. The 
examples further up on this page are a bit chaotic; they were 
experiments for me to try out the algorithm. While the algorithm 
as described there probably works, I want to change it a bit to 
allow detection for the RCArray problem.

The important thing is that the end-user doesn't have to annotate 
local variables as scope. As it's unnecessary, we can even 
consider disallowing it.

[1] 
http://wiki.dlang.org/User_talk:Schuetzm/scope2#Scope_inference


More information about the Digitalmars-d mailing list