digitalmars.D.learn - opEquals optimized away?
- Manfred Nowak (12/12) May 04 2015 class C{
- anonymous (3/15) May 04 2015 because `c is c`
- Manfred Nowak (9/10) May 05 2015 Thanks. One can see this documented in
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (12/21) May 05 2015 There is now way in D other than changing the implementation:
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 04 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? -manfredbecause `c is c` https://github.com/D-Programming-Language/druntime/blob/0ac255d7276b9b825eb6f1e677e9ee4f5ad49ca2/src/object.di#L61
May 04 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 05 2015
On 05/05/2015 04:30 PM, Manfred Nowak wrote:On Tuesday, 5 May 2015 at 05:26:17 UTC, anonymous wrote: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.)because `c is c`Thanks. One can see this documented in http://dlang.org/operatoroverloading.html#equals But 1: how can one override this behavior2: what is the design reason for thisI am guessing: - For correctness, an object should be equal to itself - Preventing memory errors when the left-hand side object is null. - Reducing boilerplateEspecially: how can one implement side effects on invoking `==' or `!='? -manfredCan you use a member function instead as in c.eq(c)? Ali
May 05 2015