Hello there,
i am having some difficulties with the safeCast operator.
I have a simple class:
class Bundle {
fun <T> getSomeGeneric(): T? =
"Hallo" as T
}
whenever i use the below code, it will still throw a java.lang.ClassCastException
fun tryOut(b: Bundle?) {
(b?.getSomeGeneric() as? Int)?.let {
println(it)
}
}
however if i use:
fun tryOut(b: Bundle?) {
b?.let { b ->
(b.getSomeGeneric() as? Int)?.let {
println(it)
}
}
}
everything works fine.
I figured that in decompiled bytecode there is no type check when using the nullcheck operator, as it gets casted within the ternary operator.
Is this behaviour intended?
Am i missing something?