5 ms·
What is an example of a use case for generics that can't be done, or is significantly less convenient to do, with interfaces? It sounds like there is at least s
by sanderjd 14y ago
What is an example of a use case for generics that can't be done, or is significantly less convenient to do, with interfaces? It sounds like there is at least some thought that generics may be included at some point, but I really haven't missed them thus far.
- lmkg 14y agoIt's not that there's a lot of things that you flat-out can't do, it's that you can't do those things in a type-safe manner. The type system isn't capable of representing what you're trying to do. This seems like an odd omission in a language that otherwise has static typing. I suspect this is why a lot of people who've jumped on the Golang bandwagon come from Python & Ruby: they're already used to no compile-time type safety, so they're not missing anything. For people who like type systems and have a style of programming that relies upon them, Go doesn't give them the tools they need to be productive. In Steve Yegge's terms: C++ is a pretty software-conservative update to C (it even has backwards-compatible syntax). Meanwhile, Go is a pretty software-liberal update (slightly to the right of Python). G simply does not support a software-conservative programming style. Consequently, Go's adoption has mostly come from software-liberal communities.
- jbooth 14y agoI hate those terms, but by his definitions, Go is statically typed and defaults to not including features if they think they might be misused. Doesn't sound that liberal to me, compared to, say, Ruby with monkey-patching.
- pufuwozu 14y agoA lot of functional constructs heavily rely on parametric polymorphism. For example: map :: (a -> b) -> [a] -> [b] Interfaces can't provide this type of code reuse.
- DanWaterworth 14y agoSlight nitpick, you can do this, just not with type safety. Type safety is the real reason generics are better than interfaces.
- mseepgood 14y agoIf you're attached to FP style you shouldn't choose an imperative language in the first place.
- jerf 14y agoIt has been pretty conclusively demonstrated at this point that a basic suite of "weak" FP tools is pretty useful in any language. No serious modern language should try to work without convenient closures and a functioning map and filter statement/function/whatever. This hasn't got anything to do with purity or academic goodness, and everything to do with the fact that languages like Python or Ruby, which are emphatically not FP languages, have simply shown such things to be indispensable tools for concise code.
- eckyptang 14y agoPretty useful: yes. Essential: no. Map/Filter introduce several new semantic concepts to a language. I think they were avoided for simplicity's sake and the fact you can achieve the same result with a for loop, even if it is slightly less concise.
- masklinn 14y ago> Map/Filter introduce several new semantic concepts to a language. No they don't. If you already have first-class functions (Go does) and some sort of sequence type (Go does), you have map/filter. There's no new semantic concepts.
- nephesh 14y agoWith this standard for "essential" you may as well just drop down into assembly language. The only added simplicity here is for the compiler writers, not the end users.
- mseepgood 14y agonumbers = map(numbers, func(n int) int { return n * n }) for i, n := range numbers { numbers[i] = n * n } Sorry, but I do not see a difference in the order of assembly language vs. high-level language.
- emu 14y agoI have to agree that the lack of generics is one of the biggest holes in Go. Common container data structures are one example. You can't write, say, a generic binary search tree in Go, unless you wrap all your data in an interface. While possible, this means you have to downcast all over the place. The built-in map, while useful, is limited in how far it goes. Maybe I'm just spoiled by the STL and the Java collections libraries.
- libria 14y agoRepository style generic data access layers: http://stackoverflow.com/questions/11914712/writing-generic-data-access-functions-in-go http://stackoverflow.com/questions/11914712/writing-generic-...
- BarkMore 14y agoThere are several generic data access layers implemented in Go: standard JSON package, Gustavo's BSON package, App Engine Data store and more. They all use the reflect package internally.
- colin_jack 14y agoI tend to use generics alongside reflection a lot, for example I might use a Mapper<TFrom, TTo> when mapping between different representations (an object and associated service contract being one example). The mapper can take care of almost everything using a few conventions and reflection, and for anything where those conventions don't cover it you plug in a little custom code (inherit from Mapper<,> or use composition). Not sure how you’d handle that sort of case in Go without generics?