alias fails to compile

aliak something at something.com
Mon Apr 22 19:57:11 UTC 2019


On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran 
wrote:
> What am I doing wrong here?
>
> struct A
> {
>     union B
>     {
>         int bb;
>     }
>     B b;
>     alias aa = B.bb;
> }
>
> void main()
> {
>     A a = A();
>     // a.b.bb = 4; // works
>     a.aa = 4; // fails
> }
>
>
> https://run.dlang.io/is/kXaVy2

You can get the behaviour you want with opDispatch, and 
generalize it with a mixin template:

mixin template AliasMember(string aliasName, string memberName) {
     ref opDispatch(string name)() if (name == aliasName) {
         return mixin(memberName);
     }
}

struct A {
     union B {
         int bb;
     }
     B b;
     mixin AliasMember!("aa", "b.bb");
}

void main() {
     A a = A();
     a.aa = 4;
}


More information about the Digitalmars-d-learn mailing list