user1277936
user1277936

Reputation:

Basic operator overloading in D (Part 2)

Using Tango with D1:

class C 
{
    private int j;
    public int opBinary(char[] op: "+") (ref C x) { return 1; }
    public int opBinary(char[] op: "+") (C x) { return 3; }
}

int opBinary(char[] op: "+") (ref C x, ref C y) { return 2; }
int opBinary(char[] op: "+") (C x, C y) { return 2; }

void main() {
    C a = new C;
    C b = new C;

    int j = a + b;
}

Compiler error:

"incompatible types"

meaning the overloaded operators weren't matched.

Can't wait to get the hang of D.

Thanks much.

OH Yea: I'm using Tango with D1, so maybe that's why it's not working? I'd like to stick with Tango. Has anyone used Tango + D2?

Upvotes: 0

Views: 219

Answers (1)

dsimcha
dsimcha

Reputation: 68770

In D1 templated operator overloading using opBinary, etc. doesn't work. You need to use opAdd, opSub, etc.

Upvotes: 3

Related Questions