I'm looking for documentation or specifications about Java generics in Kotlin.
I'm curious about the behavior that is different between the superclass and the subclass.
For example, here is the following Java code.
// A.java
public class A<T> {
public T value;
public A(T value) {
this.value = value;
}
}
// B.java
public class B<T> extends A<T> {
public B(T value) {
super(value);
}
}
Use the above Java code from Kotlin.
fun main() {
val a = A<String>("a")
val b = B<String>("b")
val aValue = getValue(a)
val bValue = getValue(b)
// lint error: Condition 'aValue == null' is always 'false'
// aValue is non-null type
println(aValue == null)
// No lint error
// bValue is platform type(String!)
println(bValue == null)
}
fun <T> getValue(a: A<T>): T {
return a.value
}
In this case, in the case of the superclass, the result of the function is marked as non-null.
On the other hand, in the case of the subclass, the result of the function is marked as platform type.
Does anyone know about this specification?
It would be helpful if you could tell me the official documents etc.