top | item 7044070

Gen - a generics library for Go

104 points| hebz0rl | 12 years ago |clipperhouse.github.io | reply

48 comments

order
[+] shurcooL|12 years ago|reply
Here is someone trying to come up with a good solution for generics, rather than simply complaining it don't exist. I am thankful for the effort.
[+] stefantalpalaru|12 years ago|reply
Good? It's an ugly hack with limited functionality (due to Go's design, not the developers' skill and good will). A good solution would be forking the reference compiler and doing a proper implementation.
[+] coldtea|12 years ago|reply
>Here is someone trying to come up with a good solution for generics, rather than simply complaining it don't exist.

That's a hack. Complaining that they don't exist might eventually result in a better language.

[+] pjmlp|12 years ago|reply
Some of us use languages with parametric types since the mid-90's.
[+] namelezz|12 years ago|reply
Just because there is no solutions yet, it does not mean you can just throw out any solution. I don't think code generation is a good solution for generics but I like the Linq feature though.
[+] stiff|12 years ago|reply
I really hope this will get solved in the language itself, I like Go quite a bit, but there are quite ugly places, all related to generics I guess:

- No min/max for integers (and Go doesn't have the ternary operator)

- No IsMember for checking if an object is in a collection

- Directly from the Gen page: Go’s sort package requires the fulfillment of three interface members, two of which are usually boilerplate. If you want to sort by different criteria, you need to implement multiple ‘alias’ types.

Also, unrelated to generics, but no multidimensional arrays.

[+] mseepgood|12 years ago|reply
> no multidimensional arrays

That's not true, e.g. [4][4]byte is a multi-dimensional array. It's a contiguous block of 16 bytes of memory.

[+] c-a|12 years ago|reply
Why call it a library when it's a code generator/compiler?
[+] mwsherman|12 years ago|reply
Author here, you’re right. I prefer to call it a tool or an ‘approach’ but whatevs. :)
[+] Refefer|12 years ago|reply
I would argue this is a new language which just happens to compile to go.
[+] LukeShu|12 years ago|reply
This looks similar to an older generics system for Go, `got` (GO Templates). Got was pretty cool, but Go 1.0 broke it due to the parser getting stricter.

Edit: found it: https://github.com/droundy/gotgo

[+] mseepgood|12 years ago|reply
These higher-order functions are a very inefficient way to write loops in Go. When you chain them you will end up with multiple sequential iterations instead of only one. Sort should not allocate and return a new slice, it's better to sort in-place, etc.
[+] mwsherman|12 years ago|reply
It’s by design, though I understand the concern. From a design perspective, I like getting a distinct slice out the other end, without having to wonder whether I’ve mutated my original slice. It’s more stateless, so to speak.

Fwiw, slices are inexpensive by design. The elements themselves are not copied. Allocating a new slice is really a small ‘struct’ which serves as a pointer to the underlying array: http://blog.golang.org/go-slices-usage-and-internals

(Gen’ing pointers is supported, which mitigates the potential for allocation.)

It is true that chained operations will be multiple iterations. Whether it will net out to more iterations than otherwise depends on the use.

I’d be interested to see if someone can come up with a pattern where the operations themselves are composed, with a ’.Results()’ method that can intelligently (lazily?) minimize iterations. That’d be impressive.

[+] rybosome|12 years ago|reply
That's functional programming for you. It's definitely less efficient, but there are other major advantages. For one thing, given the propagation of immutable data, things can be parallelized effortlessly - look at 'pmap' in Clojure for example. Once you're familiar with the style, it's significantly more concise and readable.

That being said, a systems programming language may not be the right place for FP concepts.

[+] Xelom|12 years ago|reply
Thanks for your effort. This is not about the library. Your site's mobile version is kinda broken. The left menu thingy always remain in the site and you can't read the main context. This happened to me on my Android Firefox Browser.
[+] StavrosK|12 years ago|reply
Same here, I couldn't read it at all.