Hacker News new | ask | show | jobs
by dilap 4262 days ago
(It's worth pointing out that Go does offer lots that pre-generics java doesn't, in particular interfaces w/ structural typing.)

Agreed that not having generics is a real pain-point, though not as bad as I would've assumed before using the language.

For the channel stuff, you could always just make use "chan interface{}", have your generic chan library, and then cast when receiving from the channel.

Or you could use code generation to make specific versions of your library for various types, maybe using the upcoming go generate.

It's kind of interesting, because C++ templates, for example, essentially are code-generation, but just hidden away, and for that reason tend to get overused (IMO), resulting in crazy-long compile times.

If Go's solution to generics ends up being "generate the code explicitly", it would seem uniquely Go-ish -- inelegant, but might work well practice.

(I do sort of dream about some kind of generics in Go -- something like

  where X interface{}
  type LinkedList X struct {
    Obj X
    Next *LinkedList X
  }
and maybe...

  where X Lessable
  type Heap X []X

  func (h *Heap X) Push(x X) {
     ...
  }
or even just

   where X interface{}
   func PushHeap(h []X, x X, less func (a,b X) bool) {
      ...
   }
Ah, to dream :)