aliasing expressions and identifiers

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Mon May 23 08:18:51 PDT 2016


On Monday, 23 May 2016 at 14:05:43 UTC, deed wrote:
> Some thoughts about extending the with-statement were brought 
> up here earlier:
> http://forum.dlang.org/post/txpifmwpmmhsvcpbcijw@forum.dlang.org
> I don't care much whether it would be with, alias or possibly 
> something clever already existing, but a solution should be 
> easy to use, easy to read and shouldn't introduce any possible 
> overhead at runtime.

 From the linked thread:

>        ref M()   { return matrix.rawArr; }
>        ref Ex1() { return e1.someProperties.someModulusX; }

If we had local refs, we could use this instead:

ref m = matrix.rawArr;

The difference is m is already computed, it is not recomputed 
each time m is read, unlike M(). 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.

BTW local refs are possible in @system code:
http://forum.dlang.org/post/lmuokynffgljzvrpvkvd@forum.dlang.org


More information about the Digitalmars-d mailing list