|
I think in Java, you would need to do a type-class approach. interface Numeric<T> {
T zero();
T add(T a, T b);
}
static <T> T sum(Numeric<T> n, T[] v) {
T summer = n.zero();
for (int k = 0; k < v.length; k++) {
summer = n.add(summer, v[k]);
}
return summer;
}
|
On the other hand C# offers "real" generics and you can use almost exactly the code that you wrote (generic syntax is slightly different).