[Dlang-internal] DIP1000 discussion and testing

Martin Nowak via Dlang-internal dlang-internal at puremagic.com
Tue Oct 25 00:51:21 PDT 2016


On Saturday, 22 October 2016 at 15:52:17 UTC, Mathias Lang wrote:
>> It's the same as 'return ref', which is already there, except 
>> it's 'return scope'. The examples on how it works all boil 
>> down to a couple lines of code.
>
> Most people seem to disagree with that opinion.
>
> I was looking at the DIP, and checked out the P.R. branch to 
> test it, and I cannot see how it could work if `scope` is not 
> transitive.

Let's clarify the term transitive, it's quite possible different 
people have different understandings of that term.

Only drawing on my conclusion from rewriting the RC/Unique 
implementations.
What's needed is a "container" to wrap return values so that they 
cannot be aliased or escaped, a bit similar to how you can't take 
the address of an rvalue.
If you could get a named alias (e.g. scope ref/ptr) into the 
returned scope value, then the lifetimes of the two variables are 
entangled, and it would be trivial to end up with a dangling 
scoped ref/ptr.

scope S s:
scope int* p = s.getInternalRef; // could be a malloced class ref 
instead of a pointer
if (flipCoin)
   s.destroy; // or s = S();

To make this work, we'd need to disallow any mutation of s, while 
references to it are reachable.
I wonder if that's worth the trouble over simply disallowing any 
binding to a named (and thus reachable) variable. Would require 
chaining of return values though, 
s.get.cannot.put.that.hot.potatoe.anywhere :/.


More information about the Dlang-internal mailing list