News for hackers. Stuff that matters.
Re:Generics aren't ready for Go by robmccoll on Monday February 18, 2019 @10:01AM (#19191059)
As many others have stated, there are options. In the linked list example, you could use:

- container/list.List which uses its own Element with internal pointers and interface{} for values

- write a type-specific implementation (not generic, but no runtime type assertion)

- write your own implementation using your own node / element interface type (maybe your list is generic to types that support io.Writer - generic for some use, but if you need access to specific types, you're back to runtime type assertions)

- write your own interface{} implementation (constantly needs runtime type assertions)

- use code generation (maintenance is harder, potentially non-standard tooling / build steps, but hey - no runtime type assertions)

- use a slice instead of a list (Go's version of vectors / dynamic arrays) - still requires some implementation, but the language has a lot of built-ins that make this easier. Depends on use. If you are doing a lot of insertions in the middle, this may suck, FIFO may suck and you need to be careful about not doing append()-shift and effectively leaking memory, LIFO this is dynamic and gives you good cache locality. Up shot is no-runtime type stuff and at least the slices themselves and the built-ins are generic.

There are no comments on this comment yet.