https://kotlinlang.org logo
Title
n

nickk

12/08/2017, 3:30 PM
I tried to overcome the JVM type erasure by storing the Type at compile time, like so:
data class ResultWrapper<out T>(val request: RequestInfo, val obs: T, val typeOfResult : KClass<*>? = null)
However doing a
when
on the
typeOfResult
field does not work:
when (item.typeOfResult) {
             is Enemy -> {
                  messages.add("Received instance of type ${item.typeOfResult.simpleName} !")
              }
Any ideas/suggestions?
v

Vlad

12/08/2017, 3:44 PM
enemy is not really a type of kclass. would it be just
when (item.typeOfResult) {
            Enemy::class -> {
          }
        }
?
👍 1
n

nickk

12/08/2017, 3:48 PM
This was my first reaction too, but adding
::class
produces a compile error, which I cannot understand.
d

dragas

12/08/2017, 3:50 PM
val clazz : KClass<*>? = null
    when(clazz)
    {
        String::class -> {}
    }
Are you sure? This seems to work.
n

nickk

12/08/2017, 3:50 PM
Oh, my mistake is that I should not use
is
in the check
👍 1