【发布时间】:2021-04-25 07:10:17
【问题描述】:
我有一个第三方服务,它公开了一个具体化的扩展功能。我想将此服务包装成一个接口-实现对,以便于测试和封装。
问题是我不能在我的应用程序中使用这个函数,因为编译器告诉我:
Cannot use 'T' as reified type parameter. Use a class instead.
结构如下:
interface ThirdPartyService
inline fun <reified T> ThirdPartyService.execute(): T
interface Wrapper {
fun <T> execute(): T
}
class WrapperImpl(private val thirdPartyService: ThirdPartyService) : Wrapper {
override fun <T> execute(): T =
thirdPartyService.execute()
}
在这种情况下,调用 thirdPartyService.execute() 会导致我上面提到的编译器问题。
有什么办法可以解决这个问题吗?这个Use a class instead.到底是什么意思?
【问题讨论】:
标签: kotlin generics kotlin-reified-type-parameters