aliasing/referencing expressions in with statements

deed via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 22 04:13:05 PDT 2016


On Friday, 22 April 2016 at 01:42:11 UTC, Yuxuan Shui wrote:
> Maybe use something like:
>
> auto a = () => instanceA.verboseFieldA.verboseFieldB;

You can certainly declare temporaries and rely on the compiler 
optimizing those away:

auto a = instanceA.verboseFieldA.verboseFieldB;
auto b = instanceA.aDescriptiveName;
auto value = intanceC.value;

// use a, b and value

If that were considered a sound solution, it would partly 
undermine the purpose of with statements, except for referring to 
multiple fields within the same instance, which I guess is common 
enough and makes it more convenient to express member functions 
as free functions. However, it seems simple for the compiler to 
lower such a with statement, so I wonder whether there are any 
technical reasons not to? I.e:

with (a = expressionA, b = expressionB, instanceC) {
   // access to instanceC's fields and replace a and b
   // with expressionA and expressionB, respectively.
}

That would make for very clear expressions while still allowing 
descriptive field names. Other solutions, as templates or mixins, 
seem a little too clumsy.


More information about the Digitalmars-d-learn mailing list