Understanding opAssign and 'alias this'
Maxim Fomin
maxim at maxim-fomin.ru
Mon Oct 14 08:05:32 PDT 2013
On Monday, 14 October 2013 at 10:45:26 UTC, Maurice wrote:
> 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...
> }
OK, it seems to be what actually causes the problem is implicit
conversion from B to A. Situation depends on presence of
A.opAssign(A ) because when compiler tries to resolve function
call, it is confused between calling B.opAssign(B) and casting
through alias this B to A and calling A.opAssign(A). When it
realizes that there is no best match it throws error. Since
argument B is casted to A, the error message looks so weird.
More information about the Digitalmars-d-learn
mailing list