Kotlin abstract class with generic param and methods which use type param

后端 未结 4 1619
情书的邮戳
情书的邮戳 2021-01-11 13:15

I\'m trying to create an abstract class with generic parameter which will have subclasses which should call methods without having to specify type parameters

4条回答
  •  梦毁少年i
    2021-01-11 14:07

    There are a couple of problems at play here. First, in order to retrieve a class object from a generic type parameter, you need to reify it. For this you need to declare .

    Second, you declare the generic type parameter twice. Once in the class declaration abstract class AbstractClass and once in the method declaration inline fun myMethod. Those Ts are not the same. Theoretically you could just leave out the one from the method signature but this doesn't work because reification only works with inline methods not with classes. For a possible solution to that refer to @hotkey's answer.

提交回复
热议问题