3 ms·
In one of the talks (I believe Q/A), he said "Go does have generics, they are called interfaces".
by johne20 12y ago
In one of the talks (I believe Q/A), he said "Go does have generics, they are called interfaces".
- chimeracoder 12y agoThis is a point that folks new to Go often miss. I oftentimes see people compare "interface{}" to "Object" in Java, which is not true. (If you don't believe me, note that not everything in Java is actually an Object - since there are multiple typekinds[0], that means that not every type can be used where Java expects to see an Object). That is part of the reason for the mess surrounding the so-called wrapper types like Integer (which are themselves neither objects (wrappers are passed by value, not reference) nor primitives (they are in most[1] cases unboxed to primitives, but are not themselves the same things as primitives). Aside from the fact that Java didn't add generics until 1.5 (if I remember correctly), which certainly didn't stop its popularity, it cannot be emphasized enough that the need for generics is dramatically reduced if one actually understands how to use interfaces idiomatically in Go and understands how they are different from "interfaces" in Java or other languages[2]. While I occasionally run into a situation in which Java-like generics would make things marginally easier, they're few and far between; interfaces bridge far more of the difference than non-Go programmers often realize. And has has been explained to death already (both at Gophercon and on the mailing list) - generics are not out of the question for a hypothetical Go 2, but there are going to be no more changes to the language syntax for Go 1. [0] I counted either 5 or 6 at one point when I was using Java heavily in university, but that was several years ago so don't quote me on that exact number. [1] emphasis on "most" - there are some very "fun" edge cases that you can find here! [2] I'm reminded of how Haskell has both "class" and "return" as keywords that have nothing to do with their Java counterparts - I think Go's "interface" is along the same lines - it's the same identifier/term, but a very different meaning.
- pcwalton 12y ago> This is a point that folks new to Go often miss. It's still not true that interfaces are generics, though. > Aside from the fact that Java didn't add generics until 1.5 (if I remember correctly) But that was a big mistake in Java: they had to implement them using type erasure and primitives didn't work with them (due to backwards compatibility), and they had to duplicate the collections library to have non-generic and generic collections (again due to backwards compatibility). > [2] I'm reminded of how Haskell has both "class" and "return" as keywords that have nothing to do with their Java counterparts - I think Go's "interface" is along the same lines - it's the same identifier/term, but a very different meaning. I don't think the difference is that stark—interfaces in Java and interfaces in Go are essentially the same thing. The facts that all values can be wrapped in an interface (which is also true for C#, I believe) and that interface matching is structural instead of nominal are relatively minor.