Take this simple class for three-dimensional vectors with parametrized types:
template<typename T>
class vec3 {
public:
T x;
T y;
T z;
vec3(T const& x_, T const& y_, T const& z_)
: x(x_), y(y_), z(z_) {}
vec3<T> operator + (vec3<T> const& rhs) const {
return vec3<T>(x + rhs.x, y + rhs.y, z + rhs.z);
}
};
C# allows type parametrization, but unfortunately it cannot be combined with operator overloading: you cannot tell the compiler that "these 'T' things can be added too". Java doesn't allow type parametrization over base types, or operator overloading for that matter, so it's off even worse.
C++ makes such code possible because it uses latent typing: the type of T is tracked throughout compilation, and if T doesn't support addition, a compile-time error will be issued. However, if things go wrong, this can lead to pretty astonishing compilation errors.
In the next post, I will present a proposal for adding such latent typing for C# and Java, but considerably more easily than in C++.
In the next post, I will present a proposal for adding such latent typing for C# and Java, but considerably more easily than in C++.