7 ms·
I asked this same question on reddit. And I am not trying to be snarky. I have programmed a long time and I cannot think of a single time where I thought, "this
by tonetheman 9y ago
I asked this same question on reddit. And I am not trying to be snarky. I have programmed a long time and I cannot think of a single time where I thought, "this would be easier with generics."
Where is the list of problems that are easier to solve with generics?
- andreasgonewild 9y agoYou don't mention your level of experience with generic type systems, but I find it's mostly the case that I can't imagine any use for techniques I'm not yet familiar with. Here is a random example from my recent adventures, a channel implementation in C++ which uses generics to provide value-semantics for user-specified message types: https://github.com/andreas-gone-wild/snackis/blob/master/src/snackis/core/chan.hpp https://github.com/andreas-gone-wild/snackis/blob/master/src... And here is a column class that uses generics to inject both record type and value type into the implementation: https://github.com/andreas-gone-wild/snackis/blob/master/src/snackis/db/col.hpp https://github.com/andreas-gone-wild/snackis/blob/master/src... Doing any of these without generics would mean either erasing types or writing duplicate code. Go provides generic channels, slices and maps; but it doesn't extend that power to user-code since users are supposedly not smart enough to be trusted with power.
- nine_k 9y agoChannels, maps, and arrays are generic in Go. They solve a lot if key problems. The problem is that you can't create your own generic structure. Try building a statically type-safe red-black tree, or something like that, without copy-pastig the implementation for each new node data type. Higher-order functions are also basically impossible.
- masklinn 9y ago> Channels, maps, and arrays are generic in Go. Pointers and slices as well, and of course the various builtin operators or functions used to work on them (make, range, len, close, …).