Under "No parameterized methods" 
<https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#no-parameterized-methods>,
 
the design document states:

In Go, one of the main roles of methods is to permit types to implement 
> interfaces. It is not clear whether it is reasonably possible to permit 
> parameterized methods to implement interfaces.

[...]

Or, we could decide that parameterized methods do not, in fact, implement 
> interfaces, but then it's much less clear why we need methods at all.
>

Parameterized methods that do not implement interfaces are very useful for 
expressibility, especially when it comes to fluent interfaces. The 
following is one example:

type Array(type T) []T

func (a Array(A)) Map(type B)(fn func(A) B) Array(B) {
   b := make([]B, len(a))
   for i := range a {
       b[i] = fn(a[i])
   }
   return b
}

func F() int {
   return Array(int){...}.
       Map(func(v int) int { return v*v }).
       Filter(func(v int) bool { return v%2 == 0 }).
       Sum(func(a, b int) int { return a+b })
}

For a larger example, see: 
https://gitlab.com/firelizzard/go-iter/-/blob/generic-methods/fluent/map.go2

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/29775a0d-bef7-4a5e-8ca8-83c8a7551658o%40googlegroups.com.

Reply via email to