www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Better way to achieve the following

reply JG <someone somewhere.com> writes:
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?
Jun 21 2022
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/21/22 1:09 PM, JG wrote:
 Thoughts?
Use a pointer? Especially if you are using `.method` calls, this just works seamlessly. -Steve
Jun 21 2022
parent reply JG <someone somewhere.com> writes:
On Tuesday, 21 June 2022 at 17:15:02 UTC, Steven Schveighoffer 
wrote:
 On 6/21/22 1:09 PM, JG wrote:
 Thoughts?
Use a pointer? Especially if you are using `.method` calls, this just works seamlessly. -Steve
Thanks for the suggestion. My immediate reaction is that for `.method` calls I would agree, but for assignments it is slightly less pleasant. Perhaps it is the best option.
Jun 21 2022
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/21/22 1:19 PM, JG wrote:
 On Tuesday, 21 June 2022 at 17:15:02 UTC, Steven Schveighoffer wrote:
 On 6/21/22 1:09 PM, JG wrote:
 Thoughts?
Use a pointer? Especially if you are using `.method` calls, this just works seamlessly.
Thanks for the suggestion.  My immediate reaction is that for `.method` calls I would agree, but for assignments it is slightly less pleasant. Perhaps it is the best option.
I use pointers for this purpose all the time. It's what they are for (to point at something). They even work in safe functions with dip1000 turned on. Yes it can get tedious to have to dereference in many cases. But at least if you forget and assign to `t` instead of `*t`, it probably doesn't compile. -Steve
Jun 21 2022
prev sibling next sibling parent Tejas <notrealemail gmail.com> writes:
On Tuesday, 21 June 2022 at 17:09:28 UTC, JG wrote:
 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.

 [...]
Maybe check out `std.meta.Alias`? https://dlang.org/phobos/std_meta.html#.Alias
Jun 21 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/21/22 10:09, JG wrote:
 Suppose we are often writing something like
 ```d
 theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdNa
e[theThirdIndex]=x; 
 
 ```
 One would like to something like
 ```d
 alias shortName = 
 theFirstName[theFirstIndex].theSecondName[theSecondIndex].third
ame[theThirdIndex]; 
 
 shortName = x;
 ```
 but you can't alias an expression.
An option is nested functions: ref shortName() { return theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]; } shortName = x; Ali
Jun 21 2022