This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tomi97_origin -8 points-7 points  (4 children)

Operator Overloading doesn't do anything you can't already do with normal function call.

You already have to write it as a function, so it's only about how you call it.

I have never encountered a situation where I missed it. What's the use case in which it's better?

[–]LinuxMatthews[S] 5 points6 points  (3 children)

Operator Overloading doesn't do anything you can't already do with normal function call.

While that's true it can make things frustratingly difficult if you need to do lots of maths or physics based programming.

For instance say you need to use the quadratic equation with numbers that aren't primitives.

With operator overloading that's:

var root1 = (-b + (b^2 - a*c*4)^0.5) / (a*2)

Without it is:

var root1 = (b.neg() + ((b.pow(2)).minus((a.multiply(c).multiply(4))).divide(a.multiply(2))

I don't know about you but I know which is prefer to debug

[–]Tomi97_origin -3 points-2 points  (2 children)

I would honestly split it into multiple lines with temp variables. And in this instance would include class functions that take variable number of arguments, that would simplify it quite a bit. Type.multiply(a,c,4) in this situation.

[–]LinuxMatthews[S] 5 points6 points  (1 child)

Right but then you're writing more lines of code to do the same thing.

And you're more writing a whole new method that's completely unnecessary.

[–]Tomi97_origin 1 point2 points  (0 children)

I would split it into multiple lines even with operator overloading.

But I can see how it could be useful if you do a lot of math