digitalmars.D.learn - Overloading equality operator for classes
- rumbu (19/19) Jan 29 2015 bool opEquals(Object obj, int value)
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (3/22) Jan 29 2015 UFCS is not used when operators are involved. I think this is
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/34) Jan 29 2015 In D, operator overloading is only for user-defined types and through
bool opEquals(Object obj, int value)
{
//do something to compare them
return false;
}
void main(string[] args)
{
Object obj;
if (obj == 12) {}
//ERROR function object.Object.opEquals (Object o) is not
callable using argument types (int)
}
According to paragraph (2) -
http://dlang.org/operatoroverloading.html#eqcmp), the compiler
must try obj.opEquals(12) and 12.opEquals(obj) but this is not
happening.
Is there any other way to overload the equality operator? (except
overriding opEquals in each class intended to be compared with an
integer)
Jan 29 2015
On Thursday, 29 January 2015 at 17:48:04 UTC, rumbu wrote:
bool opEquals(Object obj, int value)
{
//do something to compare them
return false;
}
void main(string[] args)
{
Object obj;
if (obj == 12) {}
//ERROR function object.Object.opEquals (Object o) is not
callable using argument types (int)
}
According to paragraph (2) -
http://dlang.org/operatoroverloading.html#eqcmp), the compiler
must try obj.opEquals(12) and 12.opEquals(obj) but this is not
happening.
Is there any other way to overload the equality operator?
(except overriding opEquals in each class intended to be
compared with an integer)
UFCS is not used when operators are involved. I think this is
intentional.
Jan 29 2015
On 01/29/2015 09:48 AM, rumbu wrote:
bool opEquals(Object obj, int value)
{
//do something to compare them
return false;
}
void main(string[] args)
{
Object obj;
if (obj == 12) {}
//ERROR function object.Object.opEquals (Object o) is not callable
using argument types (int)
}
According to paragraph (2) -
http://dlang.org/operatoroverloading.html#eqcmp), the compiler must try
obj.opEquals(12) and 12.opEquals(obj) but this is not happening.
Is there any other way to overload the equality operator? (except
overriding opEquals in each class intended to be compared with an integer)
In D, operator overloading is only for user-defined types and through
their member functions.
class C
{
int value;
bool opEquals(int value)
{
return value == this.value;
}
}
void main(string[] args)
{
auto c = new C;
if (c == 12) {}
}
Ali
Jan 29 2015









"Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> 