wck
06/11/2022, 1:54 AMwck
06/11/2022, 1:54 AMclass Test {
fun <T: Any?> nullableFun(): T {
return null as T
}
@Test
fun testNull() {
assertTrue {
val a = nullableFun<String>()
a?.length == null
}
}
@Test
fun testNull2() {
assertTrue {
nullableFun<String>()?.length == null
}
}
}
wck
06/11/2022, 1:55 AMtestNull
failed with NPE in 1.6.21, testNull2
still passedephemient
06/11/2022, 2:59 AMfun <T: Any> nullableFun(): T?
fun <T> nullableFun(): T?
because what you currently have implies that nullableFun<String>(): String
which is wrong (but can't be checked in the function itself due to generics)wck
06/11/2022, 3:11 AMwck
06/11/2022, 3:12 AMwck
06/11/2022, 3:16 AMephemient
06/11/2022, 3:33 AMval a: String =
for which the compiler will insert a check in various caseswck
06/11/2022, 3:51 AMDefinitely non-nullable types
doesn’t seem to be quite relatedephemient
06/11/2022, 3:53 AMwck
06/19/2022, 6:51 AM