I'm trying the follow
"simple string"::class.isInstance(kotlin.String)
But it return false!
I'm using
"org.jetbrains.kotlin:kotlin-reflect:1.3.21"
The another examples also fail
1::class.isInstance(kotlin.Int)
true::class.isInstance(Boolean)
Please, help me to understand it!
You use the API incorrectly, the isInstance
function does the opposite check as shown in the documentation:
Returns
true
if [value] is an instance of this class on a given platform.
The kotlin.Int
line does not refer to a type, you miss the ::class
.
A possible solution is to flip the declaration:
String::class.isInstance("a string") /// true
Int::class.isInstance(42) /// true
You may also compare KClass objects, e.g. 432::class == Int::class
or use KClass functions isSubclassOf
and isSuperclassOf