Experimenting with generics by converting a caching library to be generic

I wanted to understand go’s new generics implementation so I took a library I’ve used (GitHub - patrickmn/go-cache: An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.) and attempted to make it generic: First pass at implementing generics by lukemassa · Pull Request #147 · patrickmn/go-cache · GitHub. I’d love for feedback there/here, with the understanding that this was mostly a generics experiment for me and I don’t expect this code to be merged, at least without serious work/review from the maintainers there.

Some thoughts (somewhat overlapping with comments in the PR):

  1. Easier than expected to do the conversion; I mostly just made the main struct generic then fixed all the compile errors
  2. The ability to do type switching on parameter types (proposal: spec: generics: type switch on parametric types · Issue #45380 · golang/go · GitHub) would have made this slightly easier, however the function in question isn’t very type-safe (ideally Increment would somehow “just work” with the right constraints), so it wasn’t a big deal
  3. Similarly, naively the ability to add parameters to a method would be nice (Type Parameters Proposal), but reading up I can see how difficult that would be to support
  4. Is there a way to not say “[T comparable]” all over the place? Feels like I should be able to rely on the struct’s constraints and not open myself up to typos/requiring changes to the constraint to touch many places, but that might just be a necessity