`alias x = v.x;` not working in struct bodies?

Danilo codedan at aol.com
Sat Jan 20 09:00:22 UTC 2024


I thought this should easily work, but I was wrong:

```d
module app;
import std;

void main() {
     auto x = new Vec3(10, 20, 30);
}

struct Vec2 {
     int x, y;
}

struct Vec3 {
     Vec2 v;
     alias x = v.x;
     alias y = v.y;

     int   z;

     @disable this();

     this( typeof(x) _x, typeof(y) _y, typeof(z) _z ) { // works
         z = _z; // works

         x   = _x; // error: accessing non-static variable `x` 
requires an instance of `Vec2`
         v.x = _x; // works

         y   = _y; // error: accessing non-static variable `y` 
requires an instance of `Vec2`
         v.y = _y; // works

         writeln( x, ", ", y, ", ", z );     // error: accessing 
non-static variable `a` requires an instance of `Base`
         writeln( v.x, ", ", v.y, ", ", z ); // works
     }

     void func( typeof(x) _x, typeof(y) _y, typeof(z) _z ) { // 
works
         z = _z; // works

         // doesn't help
         //alias x = this.v.x;
         //alias y = this.v.y;

         x   = _x; // error: accessing non-static variable `x` 
requires an instance of `Vec2`
         v.x = _x; // works

         y   = _y; // error: accessing non-static variable `y` 
requires an instance of `Vec2`
         v.y = _y; // works

         writeln( x, ", ", y, ", ", z );     // error: accessing 
non-static variable `a` requires an instance of `Base`
         writeln( v.x, ", ", v.y, ", ", z ); // works
     }

}
```

Can't `alias` targets be used in method bodies?

In the method/constructor parameters it's working as expected. 
But not inside the bodies?

Documentation:
- https://dlang.org/spec/declaration.html#alias
- https://dlang.org/spec/declaration.html#alias-variable

According to the link `alias-variable` I would expect my example 
to work.
Aliasing a variable (struct member), it's a symbol.


More information about the Digitalmars-d mailing list