Understanding opAssign and 'alias this'

John Colvin john.loughran.colvin at gmail.com
Mon Oct 14 02:17:11 PDT 2013


On Sunday, 13 October 2013 at 21:37:31 UTC, Maurice wrote:
> Hello,
>
> Can anybody explain my what is happening here?
>
> enum xxx = true;
>
> struct A {
> 	static if (xxx) ref A opAssign(ref const A a) { return this; }
> 	ref A opAssign(int v) { return this; }
> }
>
> struct B {
> 	A a;
> 	alias a this;
> }
>
> void main() {
> 	A a;
> 	B b;
> 	a = b; // [1]
> 	b = a; // [2]
> }
>
> When xxx is false:
> [1] Gives an error
> [2] Compiles fine
>
> When xxx is true:
> [1] Compiles fine
> [2] Gives an error
>
> What exactly is happening here? When xxx is false, what does 'b 
> = a' do that is wrong when xxx is true?
>
> I can't find any clear documentation about opAssign and its 
> 'default' implementation.
>
> Thanks!
>
> -Maurice-

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)


More information about the Digitalmars-d-learn mailing list