I tried to overcome the JVM type erasure by storin...
# announcements
n
I tried to overcome the JVM type erasure by storing the Type at compile time, like so:
Copy code
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:
Copy code
when (item.typeOfResult) {
             is Enemy -> {
                  messages.add("Received instance of type ${item.typeOfResult.simpleName} !")
              }
Any ideas/suggestions?
v
enemy is not really a type of kclass. would it be just
Copy code
when (item.typeOfResult) {
            Enemy::class -> {
          }
        }
?
👍 1
n
This was my first reaction too, but adding
::class
produces a compile error, which I cannot understand.
d
Copy code
val clazz : KClass<*>? = null
    when(clazz)
    {
        String::class -> {}
    }
Are you sure? This seems to work.
n
Oh, my mistake is that I should not use
is
in the check
👍 1