`val actuallObj: List<*> = listOf("a", "b")`...
# getting-started
s
val actuallObj: List<*> = listOf("a", "b")
how to get the contained object type (String here) ?
n
Java has type erasure, so you can't actually do that
n
i’m not sure i understand your question as @nanodeath wrote, it’s not possible to get the type at runtime because of type erasure otherwise, it’s inferred at compile-time by the kotlin compiler
val actuallObj = listOf("a", "b")
actually, the type is
List<String>
val actuallObj: List<String> = listOf("a", "b")
actually, you’re actually removing type information with your syntax 😅
👆 1
m
You can’t for the list as a whole, but with reflection you can actually retrieve the runtime type of each element.
s
Thanks for helping out guys!! yes, I end up just check first element in my list as I knew I added them with same type. That worked out for me
n
if that works, great 👍 watch out for polymorphic types
m
@Steven Wang Just out of curiosity: if you have all elements of the same type, why can’t you declare the list with a bounded type parameter? Like
List<String>
?
s
I was using krangl, one of the column I put in data with type
LocalDateTime
krangl dataframe will assign it as
AnyCol
, it will be convenient to know which column is
LocaldateTime
which I can convert to
ZonedDateTime
and plot as x-axis
m
Oh I see, you’re constrained by a library. Thanks for explaining