Generics in C#, Java, and C++
Quote Originally Posted by Anders Hejlsberg
Yes. And we could have gone further. We did give thought to going further, but it gets very complicated. And it's not clear that the added complexity is worth the small yield that you get. If something you want to do is not directly supported in the constraint system, you can do it with a factory pattern. You could have a Matrix<T>, for example, and in that Matrix you would like to define a dot product method. That of course that means you ultimately need to understand how to multiply two Ts, but you can't say that as a constraint, at least not if T is int, double, or float. But what you could do is have your Matrix take as an argument a Calculator<T>, and in Calculator<T>, have a method called multiply. You go implement that and you pass it to the Matrix.

can anyone suggest a better way of executing Anders' suggestion than:

Code:
    public interface Calculator<T>
    {
        T multiply(T lhs, T rhs);
        T divide(T lhs, T rhs);
        T add(T lhs, T rhs);
        T subtract(T lhs, T rhs);
    };

    public class IntCalculator:Calculator<int>
    {
        public abstract int multiply(int lhs, int rhs)
        {
            return lhs * rhs;
        }
        public abstract int divide(int lhs, int rhs)
        {
            return lhs / rhs;
        }
        public abstract int add(int lhs, int rhs)
        {
            return lhs + rhs;
        }

        public abstract int subtract(int lhs, int rhs)
        {
            return lhs - rhs;
        }
    };

    public class FloatCalculator : Calculator<float>
    {
        public abstract float multiply(float lhs, float rhs)
        {
            return lhs * rhs;
        }
        public abstract float divide(float lhs, float rhs)
        {
            return lhs / rhs;
        }
        public abstract float add(float lhs, float rhs)
        {
            return lhs + rhs;
        }

        public abstract float subtract(float lhs, float rhs)
        {
            return lhs - rhs;
        }
    };

    //etc
i'm looking at extension methods, but i don't see a way to make that work with primitives...