alias fails to compile

Arun Chandrasekaran aruncxy at gmail.com
Mon Apr 22 22:24:01 UTC 2019


On Monday, 22 April 2019 at 19:57:11 UTC, aliak wrote:
> 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;
> }

Works perfect for a single member! Not otherwise.


More information about the Digitalmars-d-learn mailing list