Friday, July 31, 2015

F-Bounded Polymorphism in Java

Ok so this a fun one. Behind a WTF name lies something pretty simple.

interface Container extends Comparable<Container>

class MyContainer implements Container {
  public int compareTo(Container that) {
   ...
  }   
}

Ok so first problem, the type you're comparing is a Container so that doesn't work.
How would you compare a MyContainer and a Container.


interface Container<A> extends Comparable<A>

class MyContainer implements Container<MyContainer> {
  public int compareTo(MyContainer that) {
   ...
  }   
}

Ok so now we compare the good objects.
But by not putting any restriction on the type A, we're exposing ourselves to very weird behavior.
Someone could potentially write something like that which doesn't make any sense.
As you can see below, parameterizing the container by a String has no meaning and this should be forbidden at compile time.

interface Container<A> extends Comparable<A>

class MyContainer implements Container<String> {
  public int compareTo(String that) {
   ...
  }   
}

So this is where we're gonna use the F-Bounded Polymorphism.

interface Container<A extends Container<A>> extends Comparable<A>

class MyContainer implements Container<MyContainer> {
  public int compareTo(MyContainer that) {
   ...
  }   
}

This little recursive type definition <A extends Container<A>> means that A must be a subtype of the Container itself.

So now something like this will not even compile.
class MyContainer implements Container<String> {
  public int compareTo(String that) {
   ...
  }   
}

No comments:

Post a Comment