News for hackers. Stuff that matters.
Re:Why Go Is Not Good (2014) by justin_vanw on Wednesday December 09, 2015 @11:11AM (#10704477)
Type 1 defines: .add(x)

Type 2 defines: .plus(x)

Type 3 defines: .vector_add(x)

Now, implement a function 'average' that can work on any of these three types.

If you can get everyone in the world to agree to a convention of how to express 'addition', then there is no difference, except we already have a convention for 500 years, and it is the '+' operator. Why you think the '+' operator is confusing but .plus() is not, that is what confuses me.

Or how about 'minimum'. In C people end up writing a minimum C macro, because there's not even a way to write one function that works on int, long, float! What a sad world that is, where you have to meta program to implement min(x,y).

Re:Why Go Is Not Good (2014) by jerf on Wednesday December 09, 2015 @11:28AM (#10704616)
Operator overloading is often problematic because the operators come with semantic baggage, such as properties they maintain, and implementations of those operators often don't maintain those properties. For instance, addition is associative, and has no additional side effects beyond the value it produces, but an overloaded operator won't necessarily implement those. Abstractly there's nothing wrong with that, but in reality it has proved difficult. Operators also often come with an order-of-operation hierarchy designed for mathematical operations that map poorly or confusingly to what the overloaded operator is doing, which causes further practical mismatch.

Operator overloading works best when being used in domains where the original constraints hold; for instance, adding a matrix to a matrix is mostly the same as adding two numbers, though if your type system can't enforce that the matrices are the same size at compile time, you still added the ability for + to throw an exception, which it will never do with ints. Operator overloading got its bad name from cases where people were overloading the operators to do something entirely unlike what the original operator did, causing a mismatch between the user's expectations and what it actually did and therefore bugs.

Operator overloading isn't really "right" or "wrong" per se, but it's probably a bad idea for anything that isn't able to fully implement the contract of the operator, including "associativity", "no side effects", whether exceptions can be thrown, etc.

If you read carefully, you'll generally see operator overloading arguments have two groups talking past each other, one cursing things like C++ streams that basically overloaded the operators in a meaningless way for nominal convenience that causes a lot of long-term headaches, and the other praising the benefits of overloading for math, since math is the big case where it works correctly.

Back on topic, Go correctly does not have operator overloading because Go's authors, as near as I can tell, have no intention of Go being good for mathematics.

Re:Why Go Is Not Good (2014) by kllrnohj on Wednesday December 09, 2015 @11:53AM (#10704862)
> Operator overloading isn't really "right" or "wrong" per se, but it's probably a bad idea for anything that isn't able to fully implement the contract of the operator, including "associativity", "no side effects", whether exceptions can be thrown, etc.

Operators are not special here. What you are saying is basically "don't claim to implement the interface if you didn't implement it".

This exact problem exists widely in, for example, Java. How often do you see a Java object where someone overrode equals() but not hashCode()? I've seen that problem vastly more often than anyone doing something crazy with operators.

Re:Why Go Is Not Good (2014) by masklinn on Wednesday December 09, 2015 @11:43AM (#10704765)
> you still added the ability for + to throw an exception, which it will never do with ints.

Not quite true, the language could implement checked over- or under-flows (I believe Swift does)

Re:Why Go Is Not Good (2014) by jerf on Wednesday December 09, 2015 @12:31PM (#10705141)
"Not quite true, the language could implement checked over- or under-flows (I believe Swift does)"

I am one of the apparently about ten people who thinks that should be the universal default. The vast bulk of people disagree, and I was trying not to poke the sleeping dog. :)

Re:Why Go Is Not Good (2014) by masklinn on Wednesday December 09, 2015 @01:07PM (#10705483)
> I am one of the apparently about ten people who thinks that should be the universal default.

There's a handful of us, a handful!

FWIW Rust checks for overflow in debug mode, and while that's elided by default when compiling with optimisations it can be re-enabled with a -Z flag:

    > rustc test.rs
    > ./test
    thread '<main>' panicked at 'arithmetic operation overflowed', test.rs:4
    > rustc -O test.rs
    > ./test
    Overflowed!
    > rustc -O -Z force-overflow-checks=on test.rs
    > ./test
    thread '<main>' panicked at 'arithmetic operation overflowed', test.rs:4
And of course some languages bypass the whole thing by automatically promoting to dynamically sized integrals.
Re:Why Go Is Not Good (2014) by jerf on Wednesday December 09, 2015 @03:41PM (#10706574)
Well, that's that then; now I just need a Rust project and I'll start learning it. (I'm only an early adopter of languages, not a bleeding-edge adopter.)
Re:Why Go Is Not Good (2014) by Gankro on Wednesday December 09, 2015 @02:54PM (#10706249)
"everything is floats" is also technically a solution, but with a curious fail-state.
Re:Why Go Is Not Good (2014) by fixermark on Wednesday December 09, 2015 @11:19AM (#10704538)
Ah, I see what you mean. You can see this issue in action in Go's math package, where "Min" is defined on only float64.

I was lumping this in under the general category of the "Go has no generics" issue; without generics, Go can't do this anyway (and the closest solution you would have would be to define an interface Algebraic that specifies functions algebraic types need to support, then implement operations like min and average atop those interfaces). I'm still of the personal opinion that (as other commenters noted) what you gain in being able to define operator+ you lose in boost developers getting clever and implementing operator/= on paths; even if we had generics, I'd personally find a Go-like solution of declaring the function package an algebraic type had to support (via an Interface) preferable.

Re:Why Go Is Not Good (2014) by gendoikari on Wednesday December 09, 2015 @11:28AM (#10704618)
I think that if you begin with the idea that you have multiple conventions and you cannot avoid that, therefore you won't solve the problem with "+" operator. You have just one more convention. Type 1 defines: .add(x), Type 2 defines: .plus(x), Type 3 defines: operator+, ... If you assume that you can convince people to adopt a convention, you can use .add(x) and avoid the problem in the first place. Go for example tries to have always one obvious way to write things. The Go standard library it's the idiomatic Go bible.
Re:Why Go Is Not Good (2014) by coldpie on Wednesday December 09, 2015 @11:18AM (#10704534)
> Now, implement a function 'average' that can work on any of these three types.

I'm not convinced this is such a large problem that its solution is worth the myriad downsides that operator overloading is chained to. There's lots of schoolbook examples like this, but I've almost never seen operator overloading used well in practice. There's a few examples, like boost shared pointers are somewhat easier to read, but they're few and far between

Re:Why Go Is Not Good (2014) by pcwalton on Wednesday December 09, 2015 @11:24AM (#10704582)
> There's lots of schoolbook examples like this, but I've almost never seen operator overloading used well in practice.

I do graphics programming and I literally rely on it all the time.

Re:Why Go Is Not Good (2014) by woah on Wednesday December 09, 2015 @12:15PM (#10705025)
As someone above was mentioning, I don't think that go was ever intended for heavy math programming. It's a great language for writing network applications that is more performant and deployable than scripting languages, yet easier to use than low level languages. Use rust or c.
Re:Why Go Is Not Good (2014) by kllrnohj on Wednesday December 09, 2015 @11:57AM (#10704882)
Please elaborate on the myriad of downsides that operator overloading is chained to. I am honestly not aware of any downsides that are unique to operator overloading

Hint: Things like "they can be abused" or "you can do crazy things like have + return a dot product" are not unique to operators. I can very easily define .clone() in Java to return a dot product as well, or have .equals() do in-place addition.

Re:Why Go Is Not Good (2014) by innerspirit on Wednesday December 09, 2015 @11:18AM (#10704536)
At this point, operator overloading would simply be another convention, would it not? Your type would not work with mine if I used .add instead. You did not solve the problem of having to get everyone to agree on a convention.
Re:Why Go Is Not Good (2014) by gohrt on Wednesday December 09, 2015 @11:20AM (#10704548)
operator overloading is just overriding a base class (interface), plus a special and aribtrary limited syntax, plus compiler optimizations.