December 05, 2012 opEquals | ||||
---|---|---|---|---|
| ||||
interface I { bool opEquals(I i); } class C : I { bool opEquals(I i) { return true; } } void main() { I i1 = new C; I i2 = new C; assert(i1 == i2); // Assertino failure assert(i1 != i2); // Passes, although it's the opposite of what I want.. } What's missing? |
December 05, 2012 Re: opEquals | ||||
---|---|---|---|---|
| ||||
Posted in reply to deed | On Wednesday, 5 December 2012 at 00:17:05 UTC, deed wrote: > interface I > { > bool opEquals(I i); > } > > class C : I > { > bool opEquals(I i) > { > return true; > } > } > > void main() > { > I i1 = new C; > I i2 = new C; > assert(i1 == i2); // Assertino failure > assert(i1 != i2); // Passes, although it's the opposite of what I want.. > } > > What's missing? It looks like you want the opEquals to be called and it is not. Match the signature of it to what is in object.di. This may be what you want. ---------------------------------- import std.stdio; interface I { bool opEquals(Object i); } class C : I { override bool opEquals(Object i) const { writeln("Called"); return true; } } void main() { I i1 = new C; I i2 = new C; writeln(i1 == i2); writeln(i1 != i2); } |
Copyright © 1999-2021 by the D Language Foundation