成员成员的 dalias 无法编译

  • 本文关键字:成员 编译 dalias
  • 更新时间 :
  • 英文 :


有人知道为什么这不起作用吗?

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
}

2.065 dmd

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
}

最新更新