Thread overview
alias Error: need 'this'
Mar 19, 2023
bomat
Mar 19, 2023
Salih Dincer
Mar 19, 2023
bomat
Mar 19, 2023
Basile B.
Mar 19, 2023
Ali Çehreli
March 19, 2023

Hi,

I read about aliases today and thought it would be handy for shortening repeated access to struct members.
However, I'm clearly missing something.
Here's some example code:

int variableWithALongName = 42;
alias alias1 = variableWithALongName;
alias1 = 43;
assert(variableWithALongName == 43);

struct MyStruct
{
    int memberWithALongName;
}
MyStruct myStruct;
myStruct.memberWithALongName = 42;
alias alias2 = myStruct.memberWithALongName;
alias2 = 43; // does not compile, see below
assert(myStruct.memberWithALongName == 43);

It works fine with the int variable, but with the struct member I get a compilation error:

Error: need `this` for `memberWithALongName` of type `int`

What is that supposed to mean?

Thanks
bomat

March 19, 2023

On Sunday, 19 March 2023 at 11:52:50 UTC, bomat wrote:

>

It works fine with the int variable, but with the struct member I get a compilation error:

Error: need `this` for `memberWithALongName` of type `int`

What is that supposed to mean?

It is possible to achieve the convenience you want to achieve in 2 ways. One of them is to use a static member but if not, to use an alias inside the container. For example:

struct MyStruct
{
  int memberWithALongName;
  alias ln = memberWithALongName;

  static string str;
}

void main()
{
  auto myStruct = MyStruct(1);
       myStruct.ln = 2;
  alias alias2 =  MyStruct.str;
  alias2 = "2";

  import std.conv : text;
  assert(myStruct.ln.text == alias2);
}

SDB@79

March 19, 2023

On Sunday, 19 March 2023 at 12:29:19 UTC, Salih Dincer wrote:

>

It is possible to achieve the convenience you want to achieve in 2 ways. One of them is to use a static member but if not, to use an alias inside the container.

Thanks for the suggested workaround, I can live with the static solution, I guess.

I still don't understand why it's necessary, though.
Since a struct is a value type and, as I understand it, stack allocated, what difference does it make to the compiler whether I alias variableWithALongName or myStruct.memberWithALongName?
Shouldn't it be the exact same underlying mechanism?

Thanks and regards
bomat

March 19, 2023

On Sunday, 19 March 2023 at 13:49:36 UTC, bomat wrote:

>

Thanks for the suggested workaround, I can live with the static solution, I guess.

I still don't understand why it's necessary, though.
Since a struct is a value type and, as I understand it, stack allocated, what difference does it make to the compiler whether I alias variableWithALongName or myStruct.memberWithALongName?
Shouldn't it be the exact same underlying mechanism?

Thanks and regards
bomat

D aliases are for symbols, but what you try to alias is an expression.

You might feel that what you request may work, but that's only a very particular case. Generally expressions cannot be aliased because without context they become polymorphic (or erather polysemous).

March 19, 2023
On 3/19/23 06:49, bomat wrote:

> I can live with the `static`
> solution, I guess.

If you could, you would define it 'static' anyway. :) Because you highly likely needed a distinct 'variableWithALongName' member for each MyStruct object, that wouldn't work.

> Shouldn't it be the exact same underlying mechanism?

I don't know the answer. Luckily, what you describe is available in another form in the language:

    ref alias2() {
        return myStruct.memberWithALongName;
    }

Ali

Unrelated: I find 'myObject' a more correct name for a variable because if 'struct' is the definition of a type, 'myStruct' attempts to convey a misleading meaning.