How to get generic parameter class in Kotlin

后端 未结 5 800
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 09:30

Firebase\'s snapshot.getValue() expects to be called as follows:

snapshot?.getValue(Person::class.java)

However I would like t

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 10:06

    What you need is reified modifier for your generic param, you can read about it here. https://kotlinlang.org/docs/reference/inline-functions.html#reified-type-parameters So if you do something like that:

    inline fun T.logTag() = T::class.java.simpleName
    

    you will get name of the actual caller class, not "Object".

提交回复
热议问题