How to check generic type in Kotlin?

前端 未结 4 681
温柔的废话
温柔的废话 2020-12-29 21:47

I have class

class Generic()

and this code is\'t correct

fun typeCheck(s: SuperType): Unit {
               


        
4条回答
  •  难免孤独
    2020-12-29 22:30

    This is also example.

    inline fun  parseJson(body: String): T {
        // handle OkResponse only
        val klass = T::class.java
        if (klass.isAssignableFrom(OkResponse::class.java)) {
            return T::class.java.newInstance()
        }
        // handle others
        return gson.from(body, T::class.java)
    }
    

提交回复
热议问题