alias can't find symbol or can't use symbol

bauss via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 29 19:19:29 PDT 2017


On Sunday, 30 April 2017 at 00:17:37 UTC, Carl Sturtivant wrote:
> Consider the following.
>
> struct member
> {
>     int n;
> }
>
> struct outer
> {
>     member x;
>     alias x this;
>     alias n2 = n;
> }
>
> This does not compile: alias n2 = n;
> Error: undefined identifier 'n'
>
> On the other hand if change that into
>     alias n2 = x.n;
> then it does compile.
>
> void main()
> {
>     outer o;
>     o.n2 = 5;
> }
>
> Now this code doesn't compile: o.n2 = 5;
> Error: need 'this' for 'n' of type 'int'
>
> Given that one struct inside another is a static situation, 
> this seems unnecessarily strict. It's getting in the way of 
> some name management with `alias this`. What's the rationale 
> here?

What exactly did you expect here?

'n' is not in the scope of 'outer'.

'n' is in the scope of 'member'.

Of course it works with 'x.n' since 'x' points to the 'member' 
declared inside 'outer'.

I mean it would have worked with classes, but structs are 
different does not have any type of actual inheritance, which is 
what you're trying to achieve.

```
class member {
     int n;
}

class outer : member {
     alias n2 = n; // Ok ...
}
```


More information about the Digitalmars-d-learn mailing list