Understanding opAssign and 'alias this'

Maurice m-ou.se at m-ou.se
Mon Oct 14 03:45:25 PDT 2013


On Monday, 14 October 2013 at 09:32:15 UTC, Maxim Fomin wrote:
> On Monday, 14 October 2013 at 09:17:12 UTC, John Colvin wrote:
>> Everything is working fine except for the error on [2] when 
>> xxx == true, which I think is a bug.
>>
>> minimised test:
>>
>> struct A
>> {
>>        void opAssign(A a) {}
>> }
>>
>> struct B {
>>        A a;
>>        alias a this;
>> }
>>
>> void main() {
>>        A a;
>>        B b;
>>        b = a;
>> }
>>
>> Error: function assign.B.opAssign (B p) is not callable using 
>> argument types (A)
>
> This does not work (and need not) because compiler generates 
> default function B.opAssign(B) which is really not callable 
> using argument types (A).

Then why does it work when replacing "opAssign(A a)" with 
"opAssign(int)"?

struct A {
         void opAssign(int) {}
}

struct B {
         A a;
         alias a this;
}

void main() {
         A a;
         B b;
         b = a; // This now compiles fine...
}



More information about the Digitalmars-d-learn mailing list