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;
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/lmuokynffgljzvrpv...@forum.dlang.org
Just to be clear: I'm not looking for anything touching the
semantics, just a simple replacement mechanism/macro expansion.
Recomputation or not, @safe or @system, should be no different
from the expanded code. So within a scope, after aliasing 'm',
'm' should be replaced by 'matrix.rawArr'. Everything stays the
same as is, even error messages.
{
// Some scope
alias i = instance.targetIdx;
alias m = matrix.rawArr;
m[i] = m[j] + m[k];
// detected and conceptually replaced in one of the earlier
compiler passes by
// matrix.rawArr[instance.targetIdx] = matrix.rawArr[j] +
matrix.rawArr[k]
}