Thread overview
opEquals optimized away?
May 05, 2015
Manfred Nowak
May 05, 2015
anonymous
May 05, 2015
Manfred Nowak
May 06, 2015
Ali Çehreli
May 05, 2015
class C{
  override bool opEquals( Object o){
    return true;
  }
}
unittest{
  auto c= new C;
  assert( c == c);
}

`rdmd --main -unittest -cov' shows, that opEquals is not executed. Why?

-manfred
May 05, 2015
On Tuesday, 5 May 2015 at 04:09:03 UTC, Manfred Nowak wrote:
> class C{
>   override bool opEquals( Object o){
>     return true;
>   }
> }
> unittest{
>   auto c= new C;
>   assert( c == c);
> }
>
> `rdmd --main -unittest -cov' shows, that opEquals is not executed. Why?
>
> -manfred

because `c is c`

https://github.com/D-Programming-Language/druntime/blob/0ac255d7276b9b825eb6f1e677e9ee4f5ad49ca2/src/object.di#L61
May 05, 2015
On Tuesday, 5 May 2015 at 05:26:17 UTC, anonymous wrote:

> because `c is c`

Thanks. One can see this documented in
  http://dlang.org/operatoroverloading.html#equals

But
1: how can one override this behavior
2: what is the design reason for this

Especially: how can one implement side effects on invoking `==' or `!='?

-manfred
May 06, 2015
On 05/05/2015 04:30 PM, Manfred Nowak wrote:
> On Tuesday, 5 May 2015 at 05:26:17 UTC, anonymous wrote:
>
>> because `c is c`
>
> Thanks. One can see this documented in
>    http://dlang.org/operatoroverloading.html#equals
>
> But
> 1: how can one override this behavior

There is now way in D other than changing the implementation:


https://github.com/D-Programming-Language/druntime/blob/master/src/object_.d#L141

(There is also object.di there but I think changing the function above should be sufficient.)

> 2: what is the design reason for this

I am guessing:

- For correctness, an object should be equal to itself

- Preventing memory errors when the left-hand side object is null.

- Reducing boilerplate

> Especially: how can one implement side effects on invoking `==' or `!='?
>
> -manfred

Can you use a member function instead as in c.eq(c)?

Ali