Hacker News new | ask | show | jobs
by jcelerier 2925 days ago
but go already has generics https://www.reddit.com/r/rust/comments/5penft/parallelizing_...
1 comments

Go actually has generics.

Just not for user-defined methods and types.

If you're referring to the map type, that is implemented in plain Go. (Though it does import "unsafe") . There are no generics like you would see in other languages.

https://golang.org/src/runtime/hashmap.go

https://dave.cheney.net/2018/05/29/how-the-go-runtime-implem...

Well, it's not quite fair to say it's "plain Go." The compiler converts expressions like x, ok := m["foo"] into function calls. User-level code can't define syntax sugar like that. Also, the hashmap implementation imports some internal packages, so you can't just copy and paste it. (I would know, because I mostly did copy and paste it for one of my own projects: https://github.com/lukechampine/randmap)
When discussing whether go has generics, "plain go" is a reasonable thing to say.

Besides it's a compiler. It's going to take an input in format X and translate it to an output in format Y. That's just what they do. Syntactic sugar debates aside there's still no generics in there.

"generics" is mostly a matter of type system, not of implementation - as Java and this Go implementation showcase. Indeed, "untyped" generics is trivially implementable in Go, via `interface {}`, you just loes all the type safety (and you have to write more verbose code).

My point is, Go type system DOES support generics, but only for "primitive" types, not for user-defined types.