> [!question] > Is generic method possible in go? It seems very difficult, but I also learned that in C++ this is possible. > > This is not possible - [link](https://go.dev/doc/faq#generic_methods), [link](https://go.googlesource.com/proposal/+/master/design/43651-type-parameters.md#no-parameterized-methods) > > This makes certain form of generic libraries difficult / impossible. For example, it discourages [[Fluent Interface]]. `collection.map(x to y).map(y to z)` is not allowed. > > Noe that this is [[Dynamic Dispatch]] and [[Generics]] conflicting; Rust allows generic methods, but these methods cannot participate in Traits. Go doesn't have any annotations to help with this; thus we're stuck without it. # Documentations - https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md - https://go.dev/doc/faq#generics_comparison # Prior to Generics (<1.18) Closures allow you to get away with not having generics; but only to a certain amount. Pre-generics go *really* forces you to code unnaturally; examples; - [link](https://pkg.go.dev/sort#Interface) `sort` package; one needs to provide an interface with `Len`, `Swap`, and `Less`. - `container/heap`; the interface surface area is even bigger. - even with all this, the worst thing is that the interface isn't even *typesafe*. Generics *without* type inferencing isn't that bad; for the most part; the most useful place for generics is for containers (slices, maps, and channels were already generic; however, one couldn't make a typesafe custom data structures) - stacks and queues could be *trivially* implemented via slices, so it didn't matter too much. - lack of heap is quite annoying, especially as `container/heap` is so unergonomic. - lack of treemap is quite annoying, especially in the context of [[Leetcode]] / programming interviews. # With Generics - `slices` package - language built-in `min` / `max`. # `rangefunc` experiment # No Generic Methods Go Reification only happens at the type and function level; it cannot happen at the method level.