by foldr · 4y ago · 0 comments
I understand what people mean by this, but it's not true for any reasonable notion of 'generic'. Pre-generics Go no more has generic maps than C has generic arrays. Yes, you can declare a map or a slice of values of any given type, but you can't write a generic function that abstracts over these types. That's what is usually meant by 'generics'.
This is how built in collection types work in pretty much all statically typed languages. For example, here is an array of ints in C:
int foo[] = { 1, 2, 3 };
We wouldn't usually say that C has 'generic arrays'.> any new type created by a mere programmer has to be monomorphic.
Collections in (pre-Generics) Go are monomorphic. []foo and []bar are as different as foo and bar. That is why, for example, the first argument of sort.Slice has type 'any' (https://cs.opensource.google/go/go/+/go1.18.2:src/sort/slice...)
But was the built-in append function generic pre generics? I think it is?
func intLen(foo []int, lenfunc func ([]int) int) int {
return lenfunc(foo)
}
intLen([]int{1,2,3}, len)
There is just no sense in which Go 1.17 has “generic types” and (e.g.) C doesn’t. All statically typed languages allow you to define arrays of different types and use the same operators on these arrays. Go adds a handful of additional built in types (slices, maps, channels).The idea that some completely ordinary language features somehow constitute a crippled version of generics that has been 'turned off' for regular users seems to be an artefact of certain communities' hobbyhorsing over Go's (former) lack of generics.