If I try to access the javaClass of a generic type T the Kotlin compiler complains that T is not a subtype of kotlin.Any
class Foo<T> (val t: T ){
val cls = t.javaClass // Error, T is not a subtype of kotlin.Any
}
If define T as a subtype of Any everything works ok.
class Bar<T:Any> (val t: T ){
val cls = t.javaClass // OK
}
Q1) If type ´T´ is not a subtype of ´Any´ which class/classes can it be a subtype of ?
Q2) Do a javaClass exist for all instances of T and if so how do I access it ?
When we define a collection with "*", it should contain the object of only that type. There should not be any mix and match between the data types inside a collection. If we use "Any", we can mix and match the data types, which means we can have multiple data types in a collection.
Generics are the powerful features that allow us to define classes, methods and properties which are accessible using different data types while keeping a check of the compile-time type safety. Creating parameterized classes – A generic type is a class or method that is parameterized over types.
In this article, we will see how we can get the type of a class that is used in Kotlin. There are no direct ways to do this in Kotlin. In order to check the generic type, we need to create an instance of the generic class<T> and then we can compare the same with our class.
Well there's no difference between the first two - they're just using different names for the type parameter ( E or T ). The third isn't a valid declaration - ? is used as a wildcard which is used when providing a type argument, e.g. List<?>
The default generic upper bound is not Any
but Any?
.
This also implies that it's not null-safe to get a javaClass
from a nullable argument.
To get a javaClass
from a generic type instance with Any?
upper bound, you can cast it to Any
:
val cls = (t as Any).javaClass //unsafe
val clsOrNull = (t as? Any)?.javaClass //safe
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