Yesterday at a seminar the presenter (Peter Sestoft) showed a small java program, with 3 classes, featuring both co-variance and contra-variance. When attempting to compile using javac, the type checker will throw a StackOverflowException.
The snippet is developed by some guys that work at Microsoft (think one was called Kennedy).
Can't find it using Google. Does anyone know the code snippet, and could you paste it here (it's max. 10 lines of code), for everyone to see? :)
It was quite fun...
Have you tried bugs.sun.com? Here's a StackOverflowError
in 5.0 only:
import java.util.*;
class Test<T extends Comparable<? super T>> {
abstract class Group<E extends Comparable<? super E>>
extends ArrayList<E>
implements Comparable<Group<? extends E>> {}
abstract class Sequence<E extends Comparable<? super E>>
extends TreeSet<E>
implements Comparable<Sequence<? extends E>> {}
public void containsCombination(SortedSet<Group<T>> groups,
SortedSet<Sequence<T>> sequences) {
foo(groups, sequences);
}
<C extends Collection<T>> void foo(SortedSet<? extends C> setToCheck,
SortedSet<? extends C> validSet) {}
}
Here's another (again 5.0 only):
class F<T> {}
class C<X extends F<F<? super X>>> {
C(X x) {
F<? super X> f = x;
}
}
Found it (asked the presenter)! It's a StackOverflowException
in both 6.0 and 7.0:
class T { }
class N<Z> { }
class C<X> extends N<N<? super C<C<X>>>> {
N<? super C<T>> cast(C<T> c) { return c; }
}
It's from Andrew Kennedy and Benjamin Pierce: On Decidability of Nominal Subtyping with Variance. International Workshop on Foundations and Developments of Object-Oriented Languages g(FOOL/WOOD'07), Nice, France 2007.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With