vendredi 11 mars 2016

kotlin reflection check nullable types

How can I test if a KType variable holds a value of a nullable kotlin type, (e.G. Int?)?

I have

var type: KType 

variable coming from a KProperty<*>.returnType and I need to detect if it is equal to certain kotlin types (Int, Long, etc). This works with:

when (type) {
    Int::class.defaultType -> ...
    Long::class.defaultType -> ...
    else -> ...
}

but this only works for non-nullable types, so the first branch does not match to Int? However I was yet unable to figure out how I could detect is type is Int? other then to obvious but not so nice

type.toString().equals("kotlin.Int?")





Aucun commentaire:

Enregistrer un commentaire