Thread overview
Overloading comparison and Equality Operators
Sep 06, 2011
d coder
Sep 06, 2011
Timon Gehr
Sep 07, 2011
Brad Roberts
Sep 07, 2011
Timon Gehr
Sep 06, 2011
Trass3r
Sep 06, 2011
Timon Gehr
Sep 07, 2011
d coder
Sep 07, 2011
d coder
September 06, 2011
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
September 06, 2011
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.
September 06, 2011
> 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'.
September 06, 2011
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.
September 07, 2011
>>> 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
September 07, 2011
> 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
September 07, 2011
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

September 07, 2011
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