by chimeracoder · 12y ago · 0 comments
> It is worth pointing out that if the Go community settles on something like this as the solution, it negates one of the underlying arguments that the Go designers have against generics in the language.
It wouldn't really negate the underlying arguments; it would just mean that the community had grown and attracted developers who may not necessarily share that part of the language creators' original vision of the language.
> But all the other solutions that are headed into that gap are just one level or another of convenience wrapper around exactly that.
Well, focusing on "the other solutions" misses two things:
First, many (I dare say most) Go developers[0] don't really use any "solution" for this other than the features built into the language itself. In other words, they're not out there writing alternative solutions because they genuinely don't perceive a lack of generics to be as big of a problem as other things, which they do write tools for.
Secondly, there is a way to use interface{} to accomplish some (not all) of the same things that generics do, while still preserving type safety. Look at the functions in this Twitter client library I wrote: https://github.com/ChimeraCoder/anaconda/blob/master/users.g...
All the endpoints use the same apiGet() function ("generic"), but they all return concrete types, and they are all typesafe - you will never get a runtime panic from any of those functions. This is more than Java's generics guarantee, due to the quirks of the way type erasure works there[1].
No, this isn't "generics", but it provides an alternative way of solving the problem most of the time (in practice), and isn't that what matters? And yes, this is not quite as clean as some languages which focus on having a very expressive and precise type system (ML, Haskell, etc.), but it provides enough type safety for me without requiring me to think consciously about the types that I am using (which I have to do when writing ML or Scala).
[0] I mean people who write primarily Go at least 4-5 days a week, for work, as opposed to people who dabble with it for side projects but primarily use other languages.
[1] TL;DR: Java's generics are converted to "Object" after compilation, which means that you can actually end up with runtime type errors that are detectable at compiletime, due to inheritance. (It's been a while since I used Java, so I won't provide an example, but they're easy to find online).
As I've been thinking about this issue, it occurs to me that there are a few problems that get wrapped up into "generics", and "generics" aren't always the only solution, and people tend to conflate all the subproblems in their head when they say "Go doesn't have generics". For instance, one aspect of "generics" is to write "general algorithms" that don't care about the underlying data structure. Interfaces basically solve that problem. You write the "general algorithm" to a specified interface, and provide things that match that interface. It's pretty much "concepts" in C++. This is what the Go community correctly refers to when it says that we don't always need "generics" and that interfaces can be used instead... there is a specific and important use case for "generics" that is indeed covered by the interfaces.
The community is correct when it points out the problems are not as large as some external people are supposing, and the external critics are also correct when they observe that not all useful use cases are covered by interfaces in Go.
And I'd say the biggest uncovered useful use case is type-safe containers that aren't slices, arrays, or maps. What's more, Go's core target, network servers, is also precisely where the runtime characteristics of many of these more exotic data structures are important in the first place. Entire scripting languages are perfectly happy with arrays and hash tables as the only two effective data structures (or even just hash tables with arrays emulated on top of them!), but they pay a level of speed a Go programmer is not necessarily willing or able to pay, because network servers very often become performance sensitive. So my opinion is that whether or not that solution is "generics", Go badly needs some solution to the type-safe container problem.
(And I consider the "you can do it with interface{}" argument to be a dodge. It's a very convenient moment to suddenly decide you don't need static typing in an otherwise statically-typed language. Why stop there with that argument?)
Cover that one use case, and what's remaining isn't worth worrying about... it's not like the world is full of languages with perfect, quirk-free generics implementations either, once you start studying them they all have issues of their own. There is a reason the Go designers are resisting them, after all.
And I can't resist generalizing a bit... what's important is not the how of a solution, but the what. If you have a problem than in some other language you'd hit with generics, then try to pick up Go, then scream because Go doesn't have generics, that's not interesting if in fact interfaces would work. The problem with "Go lacking generics" isn't, well, Go lacking generics... the correct question is, what set of problems are there than Go lacks good solutions to? And, as I show above, I'm not saying that because there are no answers to that question... I named one, after all, and there's a couple of more I could at least argue sensibly about. The point is that the conversation ought to be focused on solutions to the specific problems rather than insisting that "the stick I know from another language" has to be ported in. And, it shouldn't be focused on things that Go does indeed have perfectly cromulent solutions to, because interfaces really do cover a lot of the use cases of generics, in much the same way that in C++ templated generics can be used to implement "concepts". There's definitely some overlap here which careful thinkers should be careful to keep separate in their heads as they think about the problem.
That said, I have to admit I haven't been able to sketch a solution to my type-safe data structure problem that doesn't look an awful lot like generics. But the solution space to this problem isn't bounded by my imagination. (There's some possibility that one could restrict the "genericity" to the point where it could only be used for type-safe data structures and dodge the other issues, but I haven't had enough time to noodle that over.)
For that, my current preferred approach is to wrap a generic implementation (using interface{}) with a concrete type. Like this:
http://play.golang.org/p/LkVWkyph75
There is some boilerplate code needed for each concrete type, but it is relatively small and easy to verify.
I dare say that most people don't use generics, because most people are not library authors and abstraction builders. As soon as you get into that territory, the lack of generics starts to seriously hurt. Because it's one thing to build a component that's very specific to a job at hand, it's quite another to build something reusable and I'm not even thinking about reusability by other people.
One example would be data-structures. Sure you can build your own concurrent data-structure for moving bytes between threads, sprinkled with domain specific utilities that operates on it, because that's what your application requires. It's quite another job entirely to come up with a set of reusable data-structures that can be used no matter the contained values and that exposes high-level operators for manipulating them and then you end up building half-baked data-structures for each job at hand you have.
Another example is abstracting over common patterns by means of higher-order functions. Say you want to send messages between channels, threads, address spaces or what have you, in classic producer/consumer style, but without the producer carrying about whom subscribes to its notifications. Then you realize that your stream could be manipulated for fetching just the information that you want by means of `map`, `filter` or `flatMap`. Say you want to abstract away the loops one normally does with the atomic CompareAndSwap, so you think of operators like `transformAndGet(f: OldValue => NewValue)` / `getAndTransform` and `getAndExtract(f: OldValue => (Extracted, NewValue))`, which are not uncommon in other languages, so it's not like we are being original here.
Basically, every piece of abstraction that could happen with higher-order functions is overkill for a static language without generics in it. And people don't see it, because most people aren't thinking of doing abstractions by means of higher-order functions. And Go's interfaces are seriously not enough because we are talking about a different kind of polymorphism here.
And in regards to people coming up with their own solutions, I see it no different than with Perl and the lack of OOP-ish facilities baked in the language that drove people to building multiple, often shitty and incompatible alternatives. Yes, this eventually led to Moose, but the ecosystem is still plagued by older implementations, because open-source never really dies. And I would remind you that most people that were using Perl in the beginning had no use for OOP, it just so happens that people eventually did need the libraries that needed OOP, going back to my original point that it doesn't matter if most people don't feel a need for generics, the more important question being what do library authors think about it and I'm pretty sure that people publishing libraries aren't happy.
Something do(Foo foo, Bar bar) {
Object result = API.call((Object) foo, (Object) bar);
return (Something) result;
}