Android Kotlin class reified issue -
when want int value sharedpreferences getting unsupportedoperationexception
show logcat, class int
. what's wrong?
operator inline fun <reified t : any> get(@xmls xml: string, @keys key: string, defaultvalue: t? = null): t { timber.d("${t::class} + $xml + $key + $defaultvalue") return when (t::class) { string::class -> getshared(xml)?.getstring(key, defaultvalue as? string ?: "") as? t ?: "" t int::class -> { timber.d("not triggered") //<< getshared(xml)?.getint(key, defaultvalue as? int ?: -1) as? t ?: -1 t } boolean::class -> getshared(xml)?.getboolean(key, defaultvalue as? boolean == true) as? t ?: true t float::class -> getshared(xml)?.getfloat(key, defaultvalue as? float ?: -1f) as? t ?: -1f t long::class -> getshared(xml)?.getlong(key, defaultvalue as? long ?: -1) as? t ?: -1 t else -> throw unsupportedoperationexception("unknown class!") } }
outputs:
class kotlin.int + application_data + ver + null
this failing because int::class
primitive int
while t::class
boxed type java.lang.integer
. kclass
both of them looks kotlin.int
it's hard tell difference.
this works despite looking bit odd:
when (t::class) { int::class, integer::class -> }
(i left int
in there clarity, though never trigger.)
Comments
Post a Comment