4 ms·
"I think Go got generics much better. Pass an interface{LessOrEqual} to a Sort and you're golden." That's not generics but interfaces. C++ has them too... Kin
by blablabla123 14y ago
"I think Go got generics much better. Pass an interface{LessOrEqual} to a Sort and you're golden."
That's not generics but interfaces. C++ has them too... Kind of, in C++ they are called abstract classes; eventhough you need to inherit them explicitely.
This is why you need to do type assertions when using container/heap, container/list... And this is why math.Min of two integers doesn't work without any extra work. :-)
Go is pretty cool, I like it a lot. But there are fields in which interfaces are way less powerful than generics.
- ithkuil 14y agogo generics are different from abstract classes. C++ requires you to think in advance about the kind of abstract operation your class provides, and if you are adding "sort" operation later on, and you realize that in order to sort something you have to have the concept of ordering, you have to add it to all classes you want to be sortered. Go allows you to create an interface and then to create implementations of this interface for any type that already exists. Scala solves this problem by introducing implicit conversions, so it's core design is like C++/Java, but the net effect you get from that is like in golang.
- eropple 14y agoScala solves this problem better with structural typing, which is not that different from what Go does.
- blablabla123 14y agoYeah I totally agree, the Go interfaces are much more flexible than those of C++ or even Java. But please, don't call them "go generics", they are called "interfaces". ;-) http://golang.org/doc/go_faq.html#generics http://golang.org/doc/go_faq.html#generics
- joelthelion 14y agoGo doesn't have generics. It has cool interfaces, but they solve a different problem, and the lack of generics (or templates, if you prefer) is a real problem in Go.