News for hackers. Stuff that matters.
Re:When a+b+c does not equal c+b+a by amelius on Friday June 05, 2015 @09:21AM (#9665310)
Operator overloading sucks. Especially when the expected properties such as commutativity do not hold.
Re:When a+b+c does not equal c+b+a by rhpistole on Friday June 05, 2015 @09:38AM (#9665399)
Operator overloading always looks like a clean solution, but complexity finds a way to leak through.

Avoid it, unless you're really overloading things that always behave.

Re:When a+b+c does not equal c+b+a by DannoHung on Friday June 05, 2015 @09:40AM (#9665407)
Is it really overloading that's the problem here? Even if you used method names to do this, you'd still have to deal with the fact that the operation being performed is non-commutative.

I don't know that there is a way of expressing such a restriction even if you were using dependent types in such a way as to prevent shooting yourself in the foot here. Possibly you could not implement the month or year span elements altogether and only allow addition of days or seconds?

Re:When a+b+c does not equal c+b+a by erdeszt on Friday June 05, 2015 @12:57PM (#9666554)
But nested method calls don't imply commutativity while addition does. So the code is misleading.
Re:When a+b+c does not equal c+b+a by DannoHung on Friday June 05, 2015 @02:14PM (#9667035)
I don't follow. How do nested method calls fail to imply commutativity?

For example, using psuedo-Haskell notation:

    (+):: Int -> Int -> Int

    ; ncc short for non commutative combination
    ncc:: Time -> Time -> Time

    1 + 2 + 3

    ncc 03:00 (ncc 10:00 02:00)
Re:When a+b+c does not equal c+b+a by erdeszt on Friday June 05, 2015 @04:24PM (#9667887)
It could be just me but I would expect something that looks like an addition to be commutative but not arbitrary function calls. But I see what you mean that the types are not much of a help here.
Re:When a+b+c does not equal c+b+a by JoeAltmaier on Friday June 05, 2015 @09:22AM (#9665321)
Agreed. Operator overloading of arithmetic is used as the tutorial example with complex numbers. But its utility starts and ends there.

I overload conversions of type, to simplify argument passing. Maybe I shouldn't even do that.

Re:When a+b+c does not equal c+b+a by jacquesm on Monday June 08, 2015 @01:00PM (#9680426)
Vectors, Matrices count too I think.
Re:When a+b+c does not equal c+b+a by agentultra on Friday June 05, 2015 @09:29AM (#9665351)
There are some exceptions. For geometric primitives such as matrix addition I might allow it. For complex container types like a "month," it's definitely a show-stopper.
Re:When a+b+c does not equal c+b+a by codygman on Friday June 05, 2015 @02:43PM (#9667222)
what are some specific disadvantages of operator overloading in the instance of subtracting or adding months together?
Re:When a+b+c does not equal c+b+a by agentultra on Friday June 05, 2015 @04:12PM (#9667822)
It's actually a pretty bad example, apologies. Python ships with a standard library that includes modules such as datetime, timedelta, and calendar. I've been guilty of using all of them.

I can say that when your date math becomes complex enough you might want to consider avoiding them and doing the calculations yourself. In a recent project I had to integrate several systems that stored metering data in UTC with various databases in different local time zones for the purposes of billing on that data. I can say that doing the math is easier and more clear than using objects that encapsulate the data in such complex scenarios.

There is no hard-and-fast rule for me to determine when operator-overloading is inappropriate. I only have a few "smells" I use that raise the hairs on the back of my neck when I encounter them:

1. If your object has one or more boolean members

2. If your operator has to dispatch on more than 1 type

3. If your operator breaks any of the associated identities

Any of these are just warnings. In the case of matrix addition it breaks the Commutative identity of addition but is a well-defined and known property of the operator for anyone who would be reading code involving those kinds of objects that it can be justified. Dates might be defined-enough but intuition clearly get the best of the OP.

It's just a trade-off that I haven't found valuable outside of certain special cases.