Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin language get class at runtime

Let's say that we have the following:

val person = "Bill"

Could someone explain the difference between these two:

val kClass1 = person.javaClass.kotlin    

vs

val kClass2 = person::class

When I should call the one instead of the other?

Any source code example would be appreciated.

like image 647
LiTTle Avatar asked Jun 15 '17 07:06

LiTTle


1 Answers

The main reason there are two ways to achieve the same thing, namely get the Kotlin class of an object, is because prior to Kotlin 1.1, the ::class literal did not support the expression on its left-hand side. So if you're using Kotlin 1.0, your only option is .javaClass.kotlin, otherwise you're fine with either of them. This is the reason "Kotlin in Action" uses the .javaClass.kotlin syntax: it was written before the Kotlin 1.1 release.

There's also a minor difference in the types of these expressions. For example, in the following code

interface T

fun f1(x: T) = x::class
fun f2(x: T) = x.javaClass.kotlin

f1's type is KClass<out T>, but f2's type is KClass<T>. This is actually an oversight in the javaClass declaration: KClass<out T> is more correct in this case, because x's class is not necessarily T, but can also be a subclass of T.

Otherwise these two expressions (x.javaClass.kotlin and x::class) are completely equivalent in terms of produced bytecode and runtime performance. I prefer x::class because it's shorter and reads better.

like image 160
Alexander Udalov Avatar answered Oct 13 '22 20:10

Alexander Udalov