Overloading relational operators

  • Thread starter Thread starter Jon Shemitz
  • Start date Start date
J

Jon Shemitz

Why aren't overloads of relational operators like <, ==, and >= not
restricted to returning a bool type? When would you EVER want == to
return, say, a double?

The best I can imagine, here, is that I might want to compare two
numbers for equality and get an Eta object that's basically the
difference between the two numbers. This Eta object could have an
implicit conversion to bool that would let A == B work, while at the
same time exposing methods like EqualWithIn(double Tolerance) - but
what I can't see is why I wouldn't, in this case, explicitly create an
Eta object ("new Eta(A, B)") and then call methods like Equals() or
Equals(double Tolerance) .
 
Jon Shemitz said:
Why aren't overloads of relational operators like <, ==, and >= not
restricted to returning a bool type? When would you EVER want == to
return, say, a double?

The best I can imagine, here, is that I might want to compare two
numbers for equality and get an Eta object that's basically the
difference between the two numbers. This Eta object could have an
implicit conversion to bool that would let A == B work, while at the
same time exposing methods like EqualWithIn(double Tolerance) - but
what I can't see is why I wouldn't, in this case, explicitly create an
Eta object ("new Eta(A, B)") and then call methods like Equals() or
Equals(double Tolerance) .

I can't find a particular reason or justification for this, infact I don't
see anything in the spec that mentions anything about the return
type(outside of what the defaults are). I suppose this is a case of where
they decided to err on the side of flexibility. This sounds like a wonderful
thing to ask Eric Gunnerson or one of the other MS employees(Ask a language
designer, if you can find it). I suggest posting it on Eric's "Eric Should
Write A Blog Post On" thread[1] or trying to contact the ask a language
designer email on gotdotnet(I can't find it, I'm not entiretly sure it
exists anylonger). I think those are the best chances you have to get an
answer(I doubt Anders Hejlsberg is going to show up on the group to inform
us of the design reasoning anytime soon). Maybe someone with more time will
find reasoning in the spec however.

1. http://blogs.msdn.com/ericgu/archive/2004/02/02/66347.aspx

 
Back
Top