The more I learn about generic programming the more it looks to have the potential to increase programmer productivity by as much as the transition from assembler to procedural languages achieved. My experience with it is limited to C++, and I still understand too little to solve complete problems, but in this post I’ll present an example of the kind of problem that generic programming can in principle solve.
Consider the problem of scientific software, programs that need to work with floating point numbers with physical significance. The major code quality problem is that all these different quantities are not dimensionless. It is a mistake to add an acceleration to a mass but not a mistake to add two masses, two lengths, etc. More challenging, there are composite quantities, products of more basic quantities, such as force. The whole process of insuring that the quantities in a physical equation relate appropriately is called dimensional analysis. There are well known runtime solutions to this problem, but they affect efficiency, typically of major concern to scientific modeling, and furthermore, any problems are not found until the program is running. One would like to have a compile time solution.
In particular, we would like the compiler to pass expressions of the following form (need monospace font, Bryan?),
quantity<mass> m = 5.0;
quantity<acceleration> a = 9.81;
quantity<force> f = m*a;
m = m + m;
a = a - a;
and not pass expressions like these,
f = m*m; // Compiler should reject.
a = v/(t*t); // Ditto.
The challenge is to create a set of classes, functions, macros, typically templates, that enforce these restrictions and do so without any runtime efficiency penalties, as if the quantities were simple floating point types. Let’s consider this problem at the next meeting of the Philosophical Salon.
Let me know what you come up with.