Better way to achieve the following

JG someone at somewhere.com
Tue Jun 21 17:09:28 UTC 2022


Suppose we are often writing something like
```d
theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]=x;
```
One would like to something like
```d
alias shortName = 
theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex];
shortName = x;
```
but you can't alias an expression.

You can do
```d
(ref shortName) {
  shortName = x;

}(theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]);
```
but that doesn't read well since the ``definition'' of shortName 
comes at the end.

Another option is
```d
auto aliasAs(alias f,T)(ref T x) { return f(x); }

theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex].aliasAs!
(ref shorName) {
  shortName = x;
}
```

Thoughts?


More information about the Digitalmars-d-learn mailing list