257 points · by babawere · 12y ago · 257 comments
I'm surprised at the lack of progress in generics for Go. But more than progress, I'm surprised at the lack of a story about generics in Go. Yes, the FAQ waves its hands at complexity, but the lack of discussion and/or proposals puzzle me. The wiki page about it (https://code.google.com/p/go-wiki/wiki/GoVsGenerics) is tiny and feeds from this discussion (http://groups.google.com/group/golang-nuts/browse_thread/thr...). I would be much more interested in Go were there some evidence for the intent to implement generics. A related concern is that adding generics will have a significant affect on libraries and existing code, so adding generics will become harder the longer Go waits.
I would be very happy to have the Go team say: we're going to focus on adding generics to Go in 2.0 and will be considering how to get there sooner than later; that said, we don't know when 2.0 will be released, but building in generics will drive 2.0.
Note: I understand the workarounds, but they're either hacky or have terrible performance. And I also understand that users of Go say that they don't miss generics, but I'm just not comfortable believing that.
Golang doesn't have to be all things to all people, nor does it have to fulfill every programming task any one developer has.
I would be just as happy at this point to see the Golang team put generics to rest. And I like generics, and have been annoyed by their absence in Golang before!
This. The same can be said for C as well, but C is not memory safe and Go is- so it wasn't immediately obvious the same would hold for Go.
I used to use generics/templates in Java/C#/C++ a lot, and at first I missed them in Go, but now, after writing Go full time for 2+ years- I no longer miss them more than once or twice every few months... I think generics are great, but also greatly overused.
Comprehending other people's non-generic code is a giant pain in the ass compared to reading code with generics.
This should be a book or a blog. Seems like the most powerful features of languages are often the downfall of a project. Proxies based on #doesNotUnderstand: or method_missing can be awesome and they can also be misused to render a project incomprehensible. The same goes for macros in C, macros in Lisp, and generics.
Go has no such "blessed" precompiler-macro system.
Templates/macros are an ugly solution, but they are a solution.
> a variety of complex programs are being built and deployed in Golang without generics, which makes it an open question as to whether they're required at all.
And a variety of complex programs have been built and deployed in assembler. There are arguments against adding generics, but the fact that people get by without them isn't a convincing one.
>I would be just as happy at this point to see the Golang team put generics to rest.
Clarity on this point would be great. And update the FAQ.
As it is, Rust is looking better and better as Mozilla tightens it up.
EDIT: I added the Rust note after tptacek added his reply. Apologies to him.
Sure generics would be nice, but getting my shit done and moving on to the next challenge is even nicer. Hopefully some carefully thought out additions to Go will allow me to have all this goodness at some point.
Sorry for drift, but I have to put in a word for formalized pre/post conditions in Go. Really enjoyed that stuff in Eiffel, and I think it would play in well with the judicious lack of exceptions (which I like).
The same argument could be used against adding closures to Java. After all "complex programs are being built and deployed without them". And yet, everyone thinks that it's a good idea that should have happened years before.
I think the argument is a variation of "I can do stuff in assembly just fine, why I'd need a higher level language?".
>Golang doesn't have to be all things to all people, nor does it have to fulfill every programming task any one developer has.
Well, generics are pretty basic to talk about having them as "being all things to all people". It's not like the majority of users testing or using Go regularly ask for all kinds of exotic features. The vast majority, judging from the mailing list, blog posts, articles etc, just have this major gripe.
The need for generics (or macro/template-based equivalents) naturally falls out of using a statically-typed language.
I thought the whole point of picking a language like Ruby was to use a language in which you can describe what you want, instead of how you want it.
Similarly, a variety of complex programs are being built and deployed in [any other language] without Go, which makes it an open question as to whether Go is required at all.
My point isn't that Go isn't needed. It's that this argument against generics in Go is hypocritical and contradictory to the whole reason Go exists.
Then, once the GC is decided and realized, wouldn't you rather have Go self-compile so that the community could sprout experimental branches to test out different Generics approaches and designs? Especially since the Gophers themselves aren't too sure about the right approach here and want to get as much feedback as they can...
TL;DR: It seems to me that the current trajectory of focusing primarily on the GC and self-compiling is the right way to go.
I am also not a fan of hyperliteral case-by-case error checking (it reminds me of my early C code), but it's A Style, one that the Golang team enthusiastically adopts, and it's unlikely to go anywhere.
More than just the normally used: if err := foo(); err != nil; { return err }
Large code Go code basis are so much better (more maintainable) for "hyperliteral case-by-case error checking". My impression that only people who havn't written large Go projects have this complaint.
Also, there is no reason you could not define a function like:
func foobar() (fatalErr, err error)Why do people keep ignoring me when I say this? I guess the idea that the Go team are a bunch of generics-hating curmudgeons is more compelling than the reality.
I'd like them in part because it'd enable all kinds of delightful reactive functional idioms that just aren't very possible currently. On the other hand I'm accomplishing an awful lot in Go without them..
Missing generics does not mean that no alternate data structures ever get written; as you can see, there are three of them there, which is greater than zero. However, it does mean they are more painful to use, consistently, everywhere, and no matter how much we'd all like to pretend we are infinitely disciplined programmers, pain matters, and in practice programmers visibly shy away from harder things. It is simply a fact that we must deal with. Generics missing from the language are a problem because it means that doing the right thing is harder than doing the wrong thing of just, say, using the wrong data structure because it's more convenient. A language (or an API or a library) should make right things easier and wrong things harder; it is a legit criticism when the right thing is impossible and the wrong thing (extensive copy & paste) is the only choice.
Maps & slices are great and all, but they do not correctly cover all use cases, and the sort of servers that Go is otherwise very suitable for is also exactly where you might end up needing something other than those two things for performance reasons.
I'm living without generics in Go, of course, because everyone programming in Go is. But I definitely see a lot of places here and there where I'm writing 5 lines extra, 10 lines extra, here, there, a little bit everywhere, that I simply should not have to write. Or, far worse, debug. Too often in Go I'm copying & pasting code where I should have generics, too often I'm fiddling with data structures at a lower level than I really need to.
Link?
There's actually a sort of workaround that I kind of liked:
http://play.golang.org/p/LkVWkyph75
Discussion here:
https://groups.google.com/forum/#!topic/golang-nuts/UyKree3B...
The basic approach is to define most of your generic data structure to use interface {}, and then have type-specific get/put and maybe comparison. This then minimizes the boilerplate code needed while retaining type safety. Implementing the generic functions requires a little more care than usual. But I think it is one of the best approaches for the language currently.
I wouldn't say there has been a "lack of discussion" on generics at all..
This presentation also doesn't mention the concurrent GC sweep work: https://codereview.appspot.com/46430043/
http://golang.org/src/pkg/net/http/transport.go#L408
A connection pool is kept in the transport for use with HTTP keep-alive. When an http.Client makes a request using the transport, it tries to get an idle connection, failing that it starts creating a new connection. It then selects the first available connection, either the newly-dialed one, or one that becomes available in the pool. (If the latter case, it continues opening the new connection and then sticks it into the pool.)
What makes this implementation special is that its pooling is both thread-local and global, and is plugged into the runtime/garbage collector. The former makes it much much faster than any pool the user could implement, and the later means is plays nice as resources become more scarce.
I actually have improved performance of js code significantly using pools to discard/reuse object instances in js.
(Go's implementation is potentially more efficient as you're not creating the extra WeakReference<> object, as you would be in java).
I would assume that the NaCl support means that we'll be able to write things for Chromebooks relatively easily?
There are generic lists (slices & arrays), there are generic maps (dictionaries). You can build a hell of a lot on top of that.
To generalize logic, Go's interfaces are amazing.
There are some huge projects out there using Go, and not hurt by the lack of generics. Perhaps you've heard of Docker?
I work on Juju[1], a Go project that is over 200,000 lines of code. There's only a handful of places where generics would have made things a little easier, and it's never been "OMG this is so awful because we don't have generics".
And this is coming from a guy who spent his first 14 years programming in C++ and C#, so it's not like I'm not used to generics. You just write code in a different way. Sometimes you can't get around it when you need a containery class, so you use interface{} and cast the result, but that's the exception rather than the rule.
(NaCl supports 32-bit ARM, but we have no plans to support it.)"
epic facepalm
The 'android/arm' mention in the presentation refers to writing native Android apps in Go.
In any case, I'm sure ARM support will be added in time (just not for 1.3 release) if not from the core team than by outside contributors. There are quite a lot of people using Go on ARM devices.
See my comment https://news.ycombinator.com/item?id=7219792
You can check out the Makefiles in libtorrent-go[1] and torrent2http[2]
Aww. Transliterated C code does not sound like it will take full advantage of Go idioms. Plus, an automatic translator? Unless it is extremely disciplined C code, that sounds harder than just translating it by hand.
It's the only way you can port without halting ongoing development. Done it. Been paid for it. It works.
EDIT: Also, if your original is highly idiomatic, and you exploit this, you can produce idiomatic code in the target language.
Did you work on Go's translator or a different translation project?
This is the most exciting part for me! Although I wish they didn't skim so much on the technical detail here.
I'm not intimately familiar with Go implementation, but I assume that they haven't bothered implementing precise stack- and register-scanning, which means that the runtime cannot distinguish pointers from integers on the stack/in the registers. That's a really hard problem in general and solving it requires quite some compiler support and runtime overhead. It looks like that is finally planned for Go 1.3, though.
Does this also mean that GC times will likely be longer in Go 1.3? There is not much detailed info about the Go runtime (like we do have for Java for instance) so it would great for the Go maintainers to publish a more in-depth reference of the internals of the language in version 1.3 /
To clarify, Go doesn't have pointer arithmetic.
The GC has been evolving from conservative to precise, in Go 1.1 it's gotten as far as "mostly precise".
"darwin/arm, android/arm: a contributor is working on these, some way to go."
- "Clean up and document the code, add unit tests. (Target Go 1.4)"
Oh sure, add features now and add the unit tests later. :-D #joking