HeadlinesBriefing favicon HeadlinesBriefing.com

Go Proposal Adds Generic Methods After Years of Resistance

Hacker News •
×

Go's language team has proposed adding generic methods, reversing a long-held stance that the language would never support them. The proposal allows concrete methods to declare their own type parameters, using the same syntax as generic functions but with a receiver. Two proposals on the issue tracker have accumulated over 900 positive reactions from developers requesting this feature.

The constraint has been that generic interface methods are hard to implement efficiently in Go because concrete types don't declare which interfaces they implement. Without compile-time knowledge, the compiler cannot know which instantiations to generate. The new approach sidesteps this by keeping interface methods unchanged and restricting generics to concrete methods only.

Calling a generic concrete method works with explicit type arguments or through type inference. The receiver itself can be generic, enabling methods on parameterized types. Reflection won't expose generic methods since the reflect package has no mechanism for instantiating generic values or types.