Cast to left hand side

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 9 21:17:05 PST 2014


On 11/09/2014 10:59 AM, tcak wrote:

 > When I want to create a system where data types should match
 > each other automatically,

'alias this' can do that but unfortunately, the current compiler 
supports only one 'alias this' in a user-defined type. I think that 
limitation will be gone with 2.067.

Although the following functions return rvalues, 'alias this' can use 
member variables as well:

     alias myMemberVariable this;

import std.stdio;

struct A
{
     B asB()
     {
         writeln(__FUNCTION__);
         return B();
     }

     alias asB this;
}

struct B
{
     A asA()
     {
         writeln(__FUNCTION__);
         return A();
     }

     alias asA this;
}

void main()
{
     A a;
     B b;

     b = a;
     a = b;
}

Ali



More information about the Digitalmars-d-learn mailing list