https://kotlinlang.org logo
Title
c

camkadev

11/09/2018, 10:08 AM
kotlin bug?
b

Burkhard

11/09/2018, 10:12 AM
If I remember it right,
someVariable::class.java
is the same as javas
someVariable.getClass()
. So it does not resolve the type during compile time but at runtime depending on the value. Because of that
someVariable
must not be null. If you want to resolve the type statically you could use
GeneratedMessage::class
e

Egor Trutenko

11/09/2018, 10:18 AM
That's not an issue.
those
should've been smartcasted to non-nullable
c

camkadev

11/09/2018, 10:19 AM
if i use
this
hint is the same, this is just workaround
e

Egor Trutenko

11/09/2018, 10:26 AM
Well, I'm not really sure what is exactly the problem, probably it's for the sake of Java compatibility, because you may accidentally push a null and Kotlin will treat nullable type as a normal type or something. The solution for type resolution is as @Burkhard suggested, resolve the type statically, but it'll bring overheads of reifing type parameter.
c

camkadev

11/09/2018, 10:32 AM
looks like i don't understand, what exactly should i do?
e

Egor Trutenko

11/09/2018, 10:33 AM
Instead of
this::class.java.name
use
GeneratedMessage::class.java.name
n

nestserau

11/09/2018, 12:03 PM
Just use
!!
as suggested by the IDE.
Apparently it cannot be smartcasted, which is a bummer, but then it’s safe to use
!!
.
What happens BTW if you just write:
val those = this
?
e

Egor Trutenko

11/09/2018, 12:52 PM
@nestserau where would you put
!!
?
u

udalov

11/09/2018, 1:11 PM
n

nestserau

11/09/2018, 1:12 PM
@Egor Trutenko
those!!::class.java.name
But from the link above I see that it won’t help either.