opEquals for multiple types
Jonathan M Davis
jmdavisProg at gmx.com
Sun Jul 8 13:13:46 PDT 2012
On Sunday, July 08, 2012 10:56:25 Wouter Verhelst wrote:
> Hi,
>
> I'm trying to implement opEquals. I had originally done something like
>
> class foo : bar {
> bool opEquals(bar e) {
> ... code here ...
> }
> bool opEquals(foo e) {
> ... somewhat more specialized code here ...
> }
> }
>
> (where bar is an interface)
>
> but that gave me a message about how hiding Object.opEquals is
> deprecated.
>
> My next attempt was to add a
>
> bool opEquals(Object o)
>
> which would then decide which opEquals function to call based on what
> typeid() on the Object returns. But since I want to have a version
> involving an interface, that's not going to fly.
>
> What's the best way forward?
For classes, opEquals _must_ take Object. It can't take anything else. If you
want to do something differently for specific base classes, you're going to need
to deal with it within opEquals via casting. However, that's _still_ not
likely to do what you want, because D ensures that equality goes in _both_
directions, so if the types differ, opEquals must be true for both
a.opEquals(b) and b.opEquals(a). You can't have only one side deciding that
it's equal to the other. They have to agree.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list