2 ms·
I'm hoping some linters get into golangci-lint quickly to curtail some of the biggest abuses. Some of them are obvious, like: func Whatever[reader io.Reade
by jerf 5y ago
I'm hoping some linters get into golangci-lint quickly to curtail some of the biggest abuses. Some of them are obvious, like:
func Whatever[reader io.Reader](r reader) { ... }
You can tell just from the type signature that that is a useless use of generics. If it returned "reader", then that would be potentially useful, but as written that is useless.
There will be some other things that can be caught too, potentially.
- masklinn 5y ago> You can tell just from the type signature that that is a useless use of generics. It's… not, though? In my understanding Go generics are reified, so this avoids boxing the reader, and should allow for static dispatch. How useful that is can be debated, but that's hardly useless.
- mseepgood 5y agoThe design does not require a specific implementation strategy (monomorphization, boxing or other approaches are possible). It can change from Go implementation to Go implementation and from Go version to Go version.
- jerf 5y agoIt works the other way too; the implementation is not obligated to pass an interface value to a function. The interface can be monomorphized too, if the compiler saw fit, and it would be a compliant implementation. At the language level, it's a useless use of generics. Even if generics someday used a monomorphic implementation, the compiler would be able to translate those two things back and forth trivially.