D 成员别名不为';不编译

D 成员别名不为';不编译,d,D,有人知道为什么这样不行吗 struct A { int w; alias h = w; // works } struct B { A a; alias w = a.w; // doesn't } void foo() { A a; auto c = a.h; B b; b.w; // L18 } dmd 2.065 aliastest.d(18): Error: struct aliastest.B 'w' is not a

有人知道为什么这样不行吗

struct A
{
    int w;
    alias h = w; // works
}

struct B
{
    A a;
    alias w = a.w; // doesn't
}

void foo()
{
    A a;
    auto c = a.h;
    B b;
    b.w; // L18
}
dmd 2.065

aliastest.d(18): Error: struct aliastest.B 'w' is not a member
aliastest.d(18): Error: struct aliastest.B member w is not accessible
aliastest.d(18): Error: need 'this' for 'w' of type 'int'

因为它试图在静态上下文中访问a.w(与a.w相同),所以它不使用结构实例,而是使用类型。这将有助于:

struct A
{
    static int w;
    alias h = w; // works
}

struct B
{
    A a;
    // seems it is same as alias w = A.wl
    alias w = a.w; // does work too
}

void foo()
{
    B b;
    b.w = 8; // L18
}

因为它试图在静态上下文中访问a.w(与a.w相同),所以它不使用结构实例,而是使用类型。这将有助于:

struct A
{
    static int w;
    alias h = w; // works
}

struct B
{
    A a;
    // seems it is same as alias w = A.wl
    alias w = a.w; // does work too
}

void foo()
{
    B b;
    b.w = 8; // L18
}

事实上,在这种情况下,a.w会被默默地当作a.w对待。所以,没有别的方法,只能让样板方法转发。所以,没有别的方法,只能创建样板方法转发?!这取决于你真正想要什么。您可以使用
别名aB
struct中的code>,但是
A
中的所有内容都可以从
B
中访问,实际上,在这种上下文中,A.w会像A.w一样被默默地对待。所以,没有别的方法,只能让样板方法转发。所以,没有别的方法,只能创建样板方法转发?!这取决于你真正想要什么。您可以使用
别名aB
struct中的code>,但是
A
中的所有内容都可以从
B