On Monday, 23 May 2016 at 15:18:51 UTC, Nick Treleaven wrote:
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/[email protected]
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;
Note that this wouldn't work with rvalues, which `with` supports.
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.
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
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.