aliasing expressions and identifiers

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Tue May 24 06:48:41 PDT 2016


On Monday, 23 May 2016 at 17:03:32 UTC, Marc Schütz wrote:
> On Monday, 23 May 2016 at 15:18:51 UTC, Nick Treleaven wrote:
>> If we had local refs, we could use this instead:
>>
>> ref m = matrix.rawArr;
>>
>
> Note that this wouldn't work with rvalues, which `with` 
> supports.

OK. I suppose supporting local refs is a good reason not to allow 
rvalues to be passed as const ref arguments, if the function also 
returns by ref.

>> I think the reason D doesn't support local refs is because it 
>> would make it harder to design @safe, particularly with the 
>> planned @rc ref-counting. Because M() above is only a return 
>> reference, it can't live longer than the data it references. 
>> My ref m could persist longer than matrix.rawArr using (naive) 
>> reference counting.
>
> At some point during the `scope` discussion, Walter wanted to 
> allow local `ref`s, so I guess he's not opposed to the idea. As

Great :-)

> far as I understand, the compiler already supports them 
> internally, as they can result from lowering certain 
> constructs, there's just no syntax for them. They wouldn't pose 
> a problem for lifetime tracking, because they can never be 
> assigned to, only initialized once.

What about:

@safe unittest
{
	RCArray!int arr;
	ref r = arr[0];
	arr.destroy; // refcount drops to zero, arr.impl memory freed
	r++; // writes to unallocated memory
}



More information about the Digitalmars-d mailing list