-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
According to the Type parameters proposal, it is not allowed to define type parameters in methods.
This limitation prevents to define functional-like stream processing primitives, e.g.:
func (si *stream[IN]) Map[OUT any](f func(IN) OUT) stream[OUT]While I agree that these functional streams might be unefficient and Go is not designed to cover this kind of use cases, I would like to emphasize that Go adoption in stream processing pipelines (e.g. Kafka) is a fact. Allowing type parameters in methods would allow constructing DSLs that would greatly simplify some existing use cases.
Other potential use cases that would benefit from type paremeters in methods:
- DSLs for testing:
Assert(actual).ToBe(expected) - DSLs for mocking:
On(obj.Sum).WithArgs(7, 8).ThenReturn(15)
Edited by @ianlancetaylor to add: for a summary of why this has not been approved, please see https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#no-parameterized-methods .
Edited by @adonovan to add: see #49085 (comment) for a summary of the technical challenges that leave this proposal at an impasse. And be aware that the Go FAQ says "We do not anticipate that Go will ever add generic methods."