Saturday, July 28, 2007

F-bounds and wildcards

An odd one this, that doesn't fit into our current model of wildcards. First, to set the scene, remember that the Java type checker always treats wildcards as unique. So, for example, if a method requires a parameter with type Pair and we give it a Pair it will not type check (since each wildcard is capture converted to a different, fresh type variable).

However, if we have a generic class C that has an f-bounded formal type variable:

class C<X extends C<X>> ....

And we instantiate this type with wildcards, C<? extends C<?>>, it does type check. This is surprising since the type variables in the declaration are the same and yet the wildcards in the wildcard type are 'different', but here they are regarded as the same wildcard! There is no unsoundness as we can not create a type that violates the constraints of the formal parameters, but it is surprising that the type checker can apply this knowledge to the wildcard type.

Time to upgrade our model...

No comments: