<https://pl.kotl.in/zXrGRcejU> seems like a weird ...
# announcements
k
https://pl.kotl.in/zXrGRcejU seems like a weird bug with nullable generics. Try running it, then remove
reified
from the function header and run again
r
How so? With
reified
, it fails as expected. Without
reified
, it show warnings for unchecked casts.
k
I guess I would still think that
val f: String = functionThatReturnsNull()
would fail at runtime. If that function were a Java function returning
String!
, there'd be a KotlinNullPointerException here.
r
Hm, good point. I wonder why a null check intrinsic isn't generated...
k
I guess that intrinsic isn't generally "needed" unless using platform types, or for functions visible to external Java?
c
You need
<T : Any>
. Just
T
is interpreted as “any type, including a null type”. And since nullability is not a property of the JVM, it does not fail at runtime unless the compiler knows it’s not a nullable generic type
r
@kevinmost Maybe, I guess I've never bothered to learn when intrinsics are generated. I just kind of assumed they'd magically always be there when needed.
k
I thought about this a little bit more, and it seems to me like, if you're calling the method
foo<String>
(so
T
is
String
, which is a non-nullable type, of course), when you do
result as T
in the method, since the Kotlin compiler knows that
T
is non-nullable, it should generate an intrinsic non-null check for
result
right before the cast.