How can I do generic cast? I need something like: ...
# getting-started
v
How can I do generic cast? I need something like:
Copy code
inline fun <reified T> convert(text: String): Any? {
  return try {
    text to T // something bad is happening here
  } catch (e: Exception) {
    null
  }
}
s
text as T
v
Will it work like
text.toInt()
, if
T
is
Int
?
g
No, it won't, you get ClassCastException
v
So only via
when (T::class)
?