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
}
}
}
有人知道为什么会出现编译错误"Error Kotlin: Type mismatch: inferred type is T? but Nothing was expected"
吗?针对上述代码,我该如何解决此错误?