I posted a comment about this to the blog, but I released a tool which will generate type-specific source code from a generic definition. Code is at https://github.com/joeshaw/gengen
The abs example could be reduced to:
import "github.com/joeshaw/gengen/generic"
func abs(x generic.T) generic.T {
if x < 0 {
return -x
}
return x
}
You could then generate different type-specific versions:
The downside is that the API is annoyingly non-generic (a different abs variant for each type) but at least you didn't have to type it in a bunch of times.
I agree that abs() is kind of a toy example, but this approach has helped me a lot for various slice operations like indexing and deleting.
The abs example could be reduced to:
You could then generate different type-specific versions: The downside is that the API is annoyingly non-generic (a different abs variant for each type) but at least you didn't have to type it in a bunch of times.I agree that abs() is kind of a toy example, but this approach has helped me a lot for various slice operations like indexing and deleting.