I have some code that doesn't compile with JDK10.
I reduced it to the example below:
abstract class Base<C extends Comparable<C>> {
C c;
protected Base(C c) {
this.c = c;
}
}
class Derived<O extends Object> extends Base<String> {
Derived(String s) {
super(s);
}
}
class Scratch {
private static void printString(String s) {
System.out.println(s);
}
public static void main(String[] args) {
var d = new Derived("s");
printString(d.c);
}
}
When I call printString(d.c) the compiler complains with Error:(21, 22) incompatible types: java.lang.Comparable cannot be converted to java.lang.String.
If I change
class Derived<O extends Object> extends Base<String> {
to
class Derived extends Base<String> {
the code works as intended.
How can I fix the code (so that d.c is of type java.lang.String) while keeping the type parameter O on the Derived type?