I want to cast Any
to Int
by using KClass<Int>
, having a KClass<Int>
and a Any
which is actually Int
.
fun <T> cast(any: Any, clazz: KClass<*>): T = clazz.java.cast(any)
cast(0, Int::class)
However, I got this error.
java.lang.ClassCastException: Cannot cast java.lang.Integer to int
Do you know any solution except any as Int
?
dynamic is a special type in Kotlin/JS. It basically turns off Kotlin's type checker. That is needed in order to interoperate with untyped or loosely typed environments, such as the JavaScript ecosystem.
Dynamic type Being a statically typed language, Kotlin still has to interoperate with untyped or loosely typed environments, such as the JavaScript ecosystem. To facilitate these use cases, the dynamic type is available in the language: val dyn: dynamic = ...
Type check is a way of checking the type( DataType ) or Class of a particular instance or variable while runtime to separate the flow for different objects. In few languages, it's also denoted as Run Time Type Identification (RTTI) .
Kotlin is a statically typed language, which makes it different from the dynamically typed JavaScript.
The dynamic type basically turns off Kotlin's type checker: A value of the dynamic type can be assigned to any variable or passed anywhere as a parameter. Any value can be assigned to a variable of the dynamic type or passed to a function that takes dynamic as a parameter.
Kotlin Type Casting with examples. Type casting is a process of converting one data type to another type, for example – converting int to long, long to double etc. In this tutorial we will learn how to do type conversion in Kotlin.
Because the type of the parameter any is Any, it's always a reference type and primitives will be boxed. For the second parameter, it seems that Kotlin reflection will prefer primitive types to reference types, which is why Int::class.java will default to ìnt, not Integer.
And so, it's called unsafe. The unsafe cast in Kotlin is done by the infix operator as. Note that null cannot be cast to String, as this type is not nullable. If y is null, the code above throws an exception. To make code like this correct for null values, use the nullable type on the right-hand side of the cast:
Try to change your code to
fun <T: Any> cast(any: Any, clazz: KClass<out T>): T = clazz.javaObjectType.cast(any)
Because the type of the parameter any
is Any
, it's always a reference type and primitives will be boxed. For the second parameter, it seems that Kotlin reflection will prefer primitive types to reference types, which is why Int::class.java
will default to ìnt
, not Integer
. By using javaObjectType
we force the usage of the boxed reference type.
You could also use the following function definition:
inline fun <reified T: Any> cast(any: Any): T = T::class.javaObjectType.cast(any)
// usage
cast<Int>(0)
Kirill Rakhman 's answer works well for non-nullable type. But we cannot have T::class if T is nullable. So I suggest the following to deal with nullable type.
inline fun <reified T> cast(any: Any?): T = any as T
Usage
val l32: Number = cast(32L)
val ln1: Number? = cast(null) // works
val ln2: Number = cast(null) // fails at runtime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With