alias Error: need 'this'

Salih Dincer salihdb at hotmail.com
Sun Mar 19 12:29:19 UTC 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:

```d
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 at 79


More information about the Digitalmars-d-learn mailing list