We're in a curious state right now in the OO programming language arena. On the one hand, we have people who are pushing static typing further and further. On the other hand, there's a been a resurgence of interest in dynamic typing. Ruby and Python get more and more popular every day.

I don't think that these two trends are disconnected. I've run into a number of people who look at generics in Java and just say "why?" In certain domains, you want every assurance you can have. But, the conceptual overhead of what are, essentially, two completely different type schemes (parametric and subtype polymorphism) in one language is undeniable. It's like building a bridge in which all of the left-diagonal pieces are one type of metal and all of the right-diagonal pieces another. It's complicated and you end up with an incredible moiré pattern. It's rather hard for me to believe in that this is the best that we are capable of in the industry.

For me, the most interesting bits in statically typed OO programs are the places where you can use the two both forms of polymorphism to subvert static typing entirely. One of these is the boost::any class in C++.

boost::any allows you create a dynamic typing wormhole in C++. Here's how it works. boost::any is a class which holds any value object. For instance, you can create an any from a character pointer like this:


boost::any anAny("Hello There\n");

And, you can make an any from a long like this:


boost::any anotherAny(1L);

Since any is just a copy constructible value class, you can copy anys here and there:


vector<boost::any> container;

container.push_back(anAny);
container.push_back(anotherAny);

And recover the values using a cast:

long value = boost::any_cast<long> (anotherAny);

If the cast doesn't match the value in the any object a bad_any_cast exception is thrown.

The implementation of boost::any is rather tricky. It uses templated constructors and an internal "holder" template class that mixes subtype polymorphism and parametric polymorphism to allow it to vary with the type of the object it was created with. If you want to see the details, they are here in the source.

Yes, static typing can be useful, but it's nice to have an "out." The cool thing about any is that it allows you to have to tight assurance when you need it. You can't use the value of an any without casting. But, you can use it to get a bit of leverage in the moiré that would be hard to get otherwise.