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.
Car c = ...
Car movedCar = (Car) moveMeOneInch(c);
But with Java 5 we can do something like this
<T extends Moveable> T moveMeOneInch(T m) {...}
Car c = ...
Airplane p = ...
Car movedCar = moveMeOneInch(c);
Airplane movedAirplane = moveMeOneInch(p);
and get the type safety we want, because "run time is a little late to find out whether you have a landing gear". What we've done is created an "F-bound".
F-Moveable[t] = { move : int, int -> t }
F-Moveable[Car] = { move : int, int -> Car }
F-Moveable[Airplane] = { move : int, int -> Airplane }
Note the type appears on both sides.
Here's a real world example. The Collections utility class has a max method that takes in a Collection of Comparables and return the max of the collection.
public static <T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll) {...}
//Integer implements Comparable<Integer>
Collection<Integer> ints = ...
Integer i = Collections.max(ints);
Here we bound the type of the collection and the type is part of the bound for Comparable. A collection of integers, and integers are comparable to integers.
That's F-bounded polymorphism in a nutshell. Now you can say, "yea, I like Java, it supports F-bounded polymorphism."
I don't mind the WTF. I kind of like that. But you left my name off the author list. :(
Posted by: John Mitchell | October 24, 2009 at 09:01 PM
that's because i saw you were from stanford ;-). sorry about that! credit was fixed.
Posted by: tinou | October 26, 2009 at 09:48 AM
Haha, it turns out that this article apparently has a catchy title too. It made it's way to John's Programming Languages course at Stanford!
Posted by: CS242 Student | November 15, 2009 at 01:35 PM