https://kotlinlang.org logo
#announcements
Title
# announcements
k

kevinmost

09/12/2019, 7:15 PM
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

Ruckus

09/12/2019, 7:19 PM
How so? With
reified
, it fails as expected. Without
reified
, it show warnings for unchecked casts.
k

kevinmost

09/12/2019, 7:21 PM
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

Ruckus

09/12/2019, 7:22 PM
Hm, good point. I wonder why a null check intrinsic isn't generated...
k

kevinmost

09/12/2019, 7:25 PM
I guess that intrinsic isn't generally "needed" unless using platform types, or for functions visible to external Java?
c

Casey Brooks

09/12/2019, 7:25 PM
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

Ruckus

09/12/2019, 7:29 PM
@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

kevinmost

09/16/2019, 1:08 PM
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.
2 Views