An incompatible type error occurred while using code generated from the kotlin data class
If I decompile the data generated by the data class state Class file (Val B: List < array < int > >), then I get the following java code:
public final class State { private final List<? extends Integer[]> b; public State(List<? extends Integer[]> b) { this.b = b; } public final List<Integer[]> getB() { return this.b; } // ... }
If I copy / paste this java code into my IDE (IntelliJ 15), I get the following compilation errors in the getb () method:
Incompatible types. required: List<Integer[]> Found: List<? extends Integer[]>
What did I miss here? How does kotlin generate code instead of my copy / paste version?
Solution
Usually, when javac loads Class file, it will not perform a complete type check on the code in the class; It trusts the universal signature specified in the bytecode Therefore, other JVM languages can generate signatures that javac itself rejects
In this particular case, the wildcard generated by kotlin beta 4 is meaningless (integer [] is the final class, so? Extends integer [] is useless), so the current development version does not generate any wildcard in this example
More generally, our goal is to ensure that APIs written in kotlin are easily consumed from Java code. To achieve this, kotlin allows you to control where it generates wildcards This is described as here under "Java wildcard"