4 ms·
I've been writing Go for years and never came across a problem that made me want to have generics. Anyone able to give me a simple example demonstrating why Go
by Crash0v3rid3 6y ago
I've been writing Go for years and never came across a problem that made me want to have generics.
Anyone able to give me a simple example demonstrating why Go needs this?
- eweise 6y agoOne example would be to return a single value from a method containing Either the value you want, or the error. That way you could chain a bunch of method calls together and just check for errors at the end instead of checking after each method call.
- tptacek 6y agoPresumably anything you've used codegen to solve; for instance, it'll probably make SQL libraries a lot nicer to use.
- paedubucher 6y agoCode generation requires an additional build step. One thing I like about Go is that it feels like a scripting language (go run). I guess writing a numeric library, like Python's NumPy, is a lot more convenient using generics. But I've never tried...
- jacques_chester 6y agoIt depends on how you define "need". Golang could be replaced with a notation for NAND gates and still be able to "do" everything it does now. Yesterday I was in a pairing interview where I debugged and sped up some code. All-in-all I wound up with about 200 lines or so of Golang, including a lot of "yes, another loop" code. All I was doing, really, was mapping, forking and joining. Meanwhile in Java, I could have gotten most of this in about 20 lines withJava 8 Streams and .parallel(). Generics make that tractable.
- geodel 6y agoStrange. Why you keep torturing yourself with Go? Wouldn't just moving (or just staying?) on Java save you lot of grief. On the other hand a non-technical employer might force employees to new/fashionable things, so that could be the case.
- bobthebuilders 6y agoPerhaps their existing codebase is in Go and they need to use a utility. Perhaps they really like the performance of Go compared to Java. Perhaps they can't just use Java because of inertia or that no one else knows Java. Languages have warts, and wanting to remove one is not a bad idea.
- jacques_chester 6y agoNo, I had chosen Go for that problem. My reasoning was that most of the code I've read or written in the past 2 years has been Go, so it seemed like I should focus on using a language where I was warmest. But in retrospect I would have preferred Java. That's my fault.
- bcrosby95 6y agoIf you did, I wonder if you would regret choosing Java over Go for other reasons. I tend to find that no matter what language I choose, there's some parts of the code that I would prefer to be able to write in a different language.
- jacques_chester 6y ago> I tend to find that no matter what language I choose, there's some parts of the code that I would prefer to be able to write in a different language. There is no final nirvana language. That is the awful truth of existence.
- jklehm 6y ago> no final nirvana language. There isn't one...yet!
- iMerNibor 6y agoLack of typed containers is a bit annoying for someone who touches go every now and then
- 13415 6y agoI've seen so many abuses of interface{} in packages, especially for containers, that anything that can alleviate this problem is a step in the right direction. Even when they are not strictly necessary I expect generics to improve a number of packages. Then there is also the potential performance benefit, since generics should be strictly compile-time features - at least I hope that's the case for Go, I haven't checked the details of the proposal.
- Gibbon1 6y agoDumb question, if you change an interface{} in go do you have to change everything based on it?
- jimbokun 6y agoProper higher level functions like map, reduce, ...
- tptacek 6y agoOh, wow, you're right. OK, that'd be a huge win.
- kasey_junk 6y agoThis is what I assumed generics were for. I haven’t been paying attention to the proposals but given the existing golang generics don’t support proper variance should I expect it with the user defined variance?
- wyufro 6y agoI wrote an Entity-Container system that really could've benefitted from a generic Get function. Instead I had to write on for each entity-type, even though they were the same except for different types. Most places where the empty's interface is used now could benefit from it. Eg. the json package could state it always uses float64 for numbers (except when you tell it to use Number).
- gher-shyu3i 6y agoContainers, operations on streams (map, filter, etc.), optional types (although those need tagged unions, something too advanced for golang), better concurrency handling (e.g. Task types), etc.