Thread overview
Better way to achieve the following
Jun 21, 2022
JG
Jun 21, 2022
JG
Jun 21, 2022
Tejas
Jun 21, 2022
Ali Çehreli
June 21, 2022

Suppose we are often writing something like

theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]=x;

One would like to something like

alias shortName = theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex];
shortName = x;

but you can't alias an expression.

You can do

(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

auto aliasAs(alias f,T)(ref T x) { return f(x); }

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

Thoughts?

June 21, 2022

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

June 21, 2022

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.

June 21, 2022

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

June 21, 2022

On Tuesday, 21 June 2022 at 17:09:28 UTC, JG wrote:

>

Suppose we are often writing something like

theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]=x;

One would like to something like

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

June 21, 2022
On 6/21/22 10:09, 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.

An option is nested functions:

  ref shortName() {
    return theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex];
  }

  shortName = x;

Ali