July 23, 2009

WTF is F-Bounded Polymorphism

Here's an example of where I read random articles just because the title is very catchy: F-Bounded Polymorphism for Object-Oriented Programming by Peter Canning, William Cook, Walter Hill, Walter Olthoff and John Mitchell. Polymorphism is already confusing enough. Just look at the Wikipedia entry on type polymorphism: "this article may be confusing or unclear to readers".

There's parametric polymorphism, let-polymorphism, impredicative polymorphism, ad-hoc polymorphism, intensional polymorphism and subtype polymorphism. So WTF is F-bounded polymorphism then. If there's no Wikipedia entry does it really exist? Despite no mention in Wikipedia F-bounded polymorphism is pretty important and I'm using it everyday without knowing it.

Lets start with "bounded quantification". With bounded quantification you can do something like this

Moveable { Moveable move(int x, int y)}

Moveable moveMeOneInch(Moveable m) {...}

The method moveMeOneInch takes in some a Moveable, moves it one inch, and returns a Moveable. It is polymorphic and will work on Cars, Trains, Points--anything that's Moveable. All good, except we'll have to do some unsafe type casting.