View mode: basic / threaded / horizontal-split · Log in · Help
February 23, 2012
Weird opEquals Problem
I have the following code which gives the same result on ldc2 and dmd.  
If I compare two objects of different classes I always get false even 
though the comparator is called.

The code:
~~~~~~~~~~~~~~~~~~
import std.stdio;

class A
{
    override bool opEquals(Object o)
    {
        writeln("In opEquals()");
        return true;
    }
}

class B
{

}

void main()
{
    auto o1 = new A();
    auto o2 = new A();
    auto o3 = new B();

    writeln(1);
    assert( o1 == o2 );
    writeln(2);
    assert( o1 == o3 ); // line 26.
}
~~~~~~~~~~~~~~~~~~~~~

The output:
~~~~~~~~~~~~~~~~~~~~~
$ dmd -w test.d && ./test
1
In opEquals()
2
In opEquals()
core.exception.AssertError@test(26): Assertion failure
----------------
./test() [0x804b246]
./test() [0x8049f32]
./test() [0x8049902]
./test() [0x804b848]
./test() [0x804b355]
./test() [0x804b88f]
./test() [0x804b355]
./test() [0x804b304]
/lib/libc.so.6(__libc_start_main+0xf3) [0xb7527483]
----------------
~~~~~~~~~~~~~~~~~~~~~

The key thing to notice is that opEquals() gets called both times.  Any 
ideas about what is happening?
Top | Discussion index | About this forum | D home