错误 Kotlin:类型不匹配:推断类型是 T?但没有预料到什么

aus*_*ser 5 generics kotlin

class MyExample {
  abstract class Field<in E, T : Any>(private val getter: (E) -> T?) {
    fun get(entity: E): T? { return getter(entity) }
  }

  interface Meta<T: Any> {
    fun getFields(): List<MyExample.Field<T, *>>
  }


  interface MetaRepository {
    fun <T : Any> getMeta(klass: KClass<T>): Meta<T>?
  }

  lateinit var metaRepository: MetaRepository

  fun <T : Any> doSomthing(entity: T?) {

    val meta = metaRepository.getMeta(entity!!::class)!!

    meta.getFields().forEach { field ->
      val fieldValue = field.get(entity) // <-- Error Kotlin: Type mismatch: inferred type is T? but Nothing was expected
      Unit
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

"Error Kotlin: Type mismatch: inferred type is T? but Nothing was expected"有谁知道为什么上面的代码会产生编译错误?我该如何解决这个错误?

Ale*_*nov 3

看着

val meta = metaRepository.getMeta(entity!!::class)!!
Run Code Online (Sandbox Code Playgroud)

您期望什么类型meta?大概Meta<T>。但如果你尝试对其进行注释,你就会发现这是错误的。它实际上被推断为Meta<out T>(基本上是因为entity实际上可能属于 的某个子类T,所以entity!!::class也是KClass<out T>)。

其论证field也是如此。Field<Nothing, out Any>field.getNothing

解决方案是具体化类型参数来代替KClass<T>

inline fun <reified T : Any> doSomthing(entity: T?) {

    val meta = metaRepository.getMeta(T::class)!!

    meta.getFields().forEach { field ->
        val fieldValue = field.get(entity!!)
        // Unit at the end isn't needed
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,getMeta它本身无法使用reified,因为它是一个接口方法,并且没有什么可以内联的,但是您可以创建一个辅助方法来简化对它的调用:

inline fun <reified T : Any> MetaRepository.getMeta1() = getMeta(T::class)

...
val meta = metaRepository.getMeta1<T>()!!
Run Code Online (Sandbox Code Playgroud)

旁注:如果您无论如何entity都不需要null(使用entity!!),则可能没有充分的理由将其类型T?改为T