Saturday, August 13, 2005

Generic vs Generative Programming

There is a difference in Generic and Generative programming, though C++ doesn't see it. Generic programming is about not having to specify the exact type when you are defining a class or function. This is a staple of the C++ STL. A vector can be a vector of ints, floats, strings, or foos: it doesn't matter. Generative programming is the ability to create code at compile time. For example, loop unrolling, fixing a buffer size, and choosing the correct version of a function to call at compile time is an example of generative programming.

C++ supports both Generic and Generative programming through the same language mechanism, templates. That sinking feeling should have come over you already. Using the same language feature for two distinct operations is never a good idea. Java does not support Generative programming, though Java 5.0 has recently introduced template support for Generic programming. One interesting language to study in this regard is Template Haskell. Template Haskell uses Haskell's module system to support Generic programming, but adds a new syntax to support Generative programming. I hope that other languages start supporting two unique syntaxes for these unique programming styles as well.

No comments: