www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Overloading comparison and Equality Operators

reply d coder <dlang.coder gmail.com> writes:
Greetings

It seems D assumes that these operators should return a bool value.
For example=C2=A0a < b is re-written as=C2=A0a.=E2=80=8BopCmp(b) < 0.
In my application, I wanted to overload comparison and equality
operators to return user defined values. Is that possible in D? If so
how?

Regards
- Puneet
Sep 06 2011
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/06/2011 04:13 PM, d coder wrote:
 Greetings

 It seems D assumes that these operators should return a bool value.
 For example a<  b is re-written as a.​opCmp(b)<  0.
 In my application, I wanted to overload comparison and equality
 operators to return user defined values. Is that possible in D? If so
 how?

 Regards
 - Puneet
That is currently impossible. =/ You'll have to use named member functions, eg. less, greater and equal.
Sep 06 2011
parent reply Brad Roberts <braddr puremagic.com> writes:
On Tuesday, September 06, 2011 9:09:06 AM, Timon Gehr wrote:
 On 09/06/2011 04:13 PM, d coder wrote:
 Greetings

 It seems D assumes that these operators should return a bool value.
 For example a<  b is re-written as a.​opCmp(b)<  0.
 In my application, I wanted to overload comparison and equality
 operators to return user defined values. Is that possible in D? If so
 how?

 Regards
 - Puneet
That is currently impossible. =/ You'll have to use named member functions, eg. less, greater and equal.
Normally I try to avoid nit picking phrasing in public forums, but in this case it's important. It's not just 'currently' impossible, it's by design with no intention to change. Later, Brad
Sep 06 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/07/2011 05:28 AM, Brad Roberts wrote:
 On Tuesday, September 06, 2011 9:09:06 AM, Timon Gehr wrote:
 On 09/06/2011 04:13 PM, d coder wrote:
 Greetings

 It seems D assumes that these operators should return a bool value.
 For example a<   b is re-written as a.​opCmp(b)<   0.
 In my application, I wanted to overload comparison and equality
 operators to return user defined values. Is that possible in D? If so
 how?

 Regards
 - Puneet
That is currently impossible. =/ You'll have to use named member functions, eg. less, greater and equal.
Normally I try to avoid nit picking phrasing in public forums, but in this case it's important. It's not just 'currently' impossible,
not 'just' currently, but I never said 'just'. ;)
 it's by design with no intention to change.
I never said there was. I wanted to express I wish there was. Timon
Sep 07 2011
prev sibling parent reply Trass3r <un known.com> writes:
 For example a < b is re-written as a.=E2=80=8BopCmp(b) < 0.
 In my application, I wanted to overload comparison and equality
 operators to return user defined values. Is that possible in D? If so
 how?
What are you trying to do? In the end opCmp can return any value or object that works in the = rewritten case 'a.opCmp(b) < 0'.
Sep 06 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/06/2011 07:21 PM, Trass3r wrote:
 For example a < b is re-written as a.​opCmp(b) < 0.
 In my application, I wanted to overload comparison and equality
 operators to return user defined values. Is that possible in D? If so
 how?
What are you trying to do? In the end opCmp can return any value or object that works in the rewritten case 'a.opCmp(b) < 0'.
Which is a fancy way of returning an int. Probably he wants to implement expression templates.
Sep 06 2011
next sibling parent d coder <dlang.coder gmail.com> writes:
 For example a < b is re-written as a.opCmp(b) < 0.
 In my application, I wanted to overload comparison and equality
 operators to return user defined values. Is that possible in D? If so
 how?
What are you trying to do? In the end opCmp can return any value or object that works in the rewritten case 'a.opCmp(b) < 0'.
Which is a fancy way of returning an int. Probably he wants to implement expression templates.
Actually not expression templates. I am porting a BDD (Binary decision diagrams) library from C++ to D. The comparison operators are just implemented as relations that return another BDD. A BDD would then evaluate to 0 or 1 depending on many other factors and other BDDs. A BDD actually represents a node on an acyclic graph. Being able to overload comparison operators more generically would have helped. For now I am using named functions as Timon suggested. Regards - Puneet
Sep 06 2011
prev sibling parent d coder <dlang.coder gmail.com> writes:
 Actually not expression templates. I am porting a BDD (Binary decision
 diagrams) library from C++ to D. The comparison operators are just
 implemented as relations that return another BDD. A BDD would then
 evaluate to 0 or 1 depending on many other factors and other BDDs. A
 BDD actually represents a node on an acyclic graph.
Excuse my poor knowledge of expression templates. It seems I am doing something similar to expression templates, but not at meta-programming level. Regards - Puneet
Sep 06 2011