Using alias in opBinary onto the object that is passed in incorrectly aliases to "this" instead of the object passed in. It's best illustrated with some code:
import std.stdio;
class Foo {
int kek;
this(int val) {
this.kek = val;
}
Foo opBinary(string op)(Foo bar) if (op == "*") {
alias k1 = this.kek;
alias k2 = bar.kek;
writefln("k1 = %s, this.kek = %s", k1, this.kek);
writefln("k2 = %s, bar.kek = %s", k2, bar.kek);
Foo f = new Foo(k1 * k2);
return f;
}
}
void main() {
Foo f1 = new Foo(2);
Foo f2 = new Foo(5);
Foo f3 = f1 * f2;
}
The output is:
k1 = 2, this.kek = 2
k2 = 2, bar.kek = 5
The expected output would be:
k1 = 2, this.kek = 2
k2 = 5, bar.kek = 5
Comment #1 by robert.schadek — 2024-12-13T18:13:14Z