Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>B) does this count as generics?

I think "generic programming" is more about generic algorithms than it is about data structures. Because without generic functions, generic data structures are less useful.

Can you create a new map type in Go? Yes. Can you create a function that works on any map? Only with reflection, which you should consider your last measure.



> Can you create a function that works on any map? Only with reflection

Most experienced go developers at this point would template the function and generate it for any type they might need it for, before they reach for reflection.


Depends on the environment, I guess. I've been programming Go professionally for over 3 years, and I've never used such "templates" for generic types.

As I've said in another thread, I've had a need for sum types much more often than a need for generics.


> template the function and generate it for any type they might need it for

How? This is C++ generics. I thought Go didn't have generics?


I think they meant literal generation of files based on templates. Basically

  sed 's/T/int/g' file@T.go > file@int.go
Some people do that, I guess.



little more 'go' than that, using go generate and the template package. There are also other generators that can generate a 'Stringer' for any type, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: