News for hackers. Stuff that matters.
Re:Eight years of Go by ori_b on Friday November 10, 2017 @01:46PM (#15671961)
But you don't need to do that. It's a bit clunkier than generics, but it's usable: https://golang.org/pkg/sort/
Re:Eight years of Go by krylon on Friday November 10, 2017 @02:22PM (#15672233)
I have had the need to sort data rarely enough that it has not been a real problem to me (most of the time that data came out of a relational database that did the sorting for me).

But when I did have the need to sort stuff, I have found it annoying. Even C has a more convenient solution for this problem. (Admittedly, Go's sort.Sort can work for data structures other than arrays/slices.)

Like I said, it has not been a sufficiently large problem to really bother me, but it is not a pretty solution, IMHO.

Of course, one might argue that the creators of Go knew that sorting things was not such a common problem for their target audience, and thus they made the trade off to make sorting suck in return for overall simplicity; Go's type system makes it practically impossible to implement a type-generic sorting function like C's qsort(3) without sacrificing performance or making the language more complex.

So maybe that is one of the trade offs we have to make. I still wish for a better solution, even if that may be impossible without turning Go into another C++. And if you like C++, that is totally fine, it has a number of very big advantages. But then you do not need Go to become another C++ if you have the original right there; and even if you wanted to get away from C++ without giving up the benefits it offers, D looks like a more promising alternative.

Re:Eight years of Go by pcwalton on Friday November 10, 2017 @02:14PM (#15672167)
You're still writing separate functions (implementations of sort.Interface) to sort different types.
Re:Eight years of Go by fauigerzigerk on Friday November 10, 2017 @03:20PM (#15672627)
For sorting slices you only have to pass one function:

  sort.Slice(s, func(i, j int) bool { return s[i].X < s[j].X })
Re:Eight years of Go by ori_b on Friday November 10, 2017 @03:04PM (#15672512)
But this is something you often need to do for any non-trivial type, even with generics. There's a reason that C++ std::sort<> takes a comparator, or why Rust requires you to implement the 'Ord' interface.

Again, it's a little clunkier in Go, but not unusably so.

Re:Eight years of Go by pcwalton on Friday November 10, 2017 @03:36PM (#15672760)
You need to define how to order things in other languages (though not in languages where you can automatically derive implementations like Haskell or languages with built-in polymorphic comparison like OCaml) but you don't need to define how to swap elements over and over.
Re:Eight years of Go by ori_b on Friday November 10, 2017 @04:49PM (#15673342)
Yes, I'm aware. That's the clunky part that I was referring to (although, you can still avoid it for slices, and simply pass a single compare function.)

Go is far from my favorite language, but the sheer amount of bad criticism by people that clearly don't use the language annoys me.

Re:Eight years of Go by dualogy on Friday November 10, 2017 @02:19PM (#15672210)
A little boilerplate hasn't killed anyone. If you're talking about "100s of different Enterprise Business Objects (TM) structs", something is probably off in the overall program design and/or code-gen should probably be introduced regardless of the 'sort' (and related typically-generics use-cases) question, as that sounds like something to be largely derived from pre-existing schemas of some sort..
Re:Eight years of Go by pcwalton on Friday November 10, 2017 @02:25PM (#15672246)
> A little boilerplate hasn't killed anyone.

It's annoying, especially since basic generics are simpler than interfaces.

> If you're talking about "100s of different Enterprise Business Objects (TM) structs"

I don't write enterprise software. I sometimes need to sort things.

Re:Eight years of Go by hood_syntax on Friday November 10, 2017 @01:50PM (#15671993)
I did a little research after I posted my original comment and came across that. It does seem usable even if it's not what I'd prefer