Re: alias Error: need 'this'

2023-03-19 Thread Ali Çehreli via Digitalmars-d-learn

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.




Re: alias Error: need 'this'

2023-03-19 Thread Basile B. via Digitalmars-d-learn

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).


Re: alias Error: need 'this'

2023-03-19 Thread bomat via Digitalmars-d-learn

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


Re: alias Error: need 'this'

2023-03-19 Thread Salih Dincer via Digitalmars-d-learn

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