www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias Error: need 'this'

reply bomat <Tempest_spam gmx.de> writes:
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
Mar 19 2023
parent reply Salih Dincer <salihdb hotmail.com> writes:
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 79
Mar 19 2023
parent reply bomat <Tempest_spam gmx.de> writes:
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
Mar 19 2023
next sibling parent Basile B. <b2.temp gmx.com> writes:
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).
Mar 19 2023
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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.
Mar 19 2023