gpt4 book ai didi

android - Kotlin 中的密封类,不兼容类型错误无法返回父类型

转载 作者:行者123 更新时间:2023-12-05 00:18:51 28 4
gpt4 key购买 nike

我有这个密封的类代 TableView 状态

sealed class ViewState<out ResultType>(
) {
data class Success<ResultType>(val data: ResultType?) : ViewState<ResultType>()
data class Error(val message: String) : ViewState<Nothing>()
object Loading : ViewState<Nothing>()

}
这里我使用 viewState
fun <T, A> performGetOperation(databaseQuery: () -> LiveData<T>)): LiveData<ViewState<T>> =
liveData(Dispatchers.IO) {
emit(ViewState.Loading)
val cache: LiveData<ViewState.Success<T>> = databaseQuery.invoke()
.map { ViewState.Success<T>(it) }

emitSource(cache)
}
这条线太疯狂了 emitSource(cache) 给我 emitSource(cache)
Required:
LiveData<ViewState<T>>
Found:
LiveData<ViewState.Success<T>>

最佳答案

这是一个简单的类型定义问题。
您定义了 cacheLiveData<ViewState.Success<T>>LiveData<ViewState<T>> 的返回类型不匹配.
您必须从 val cache: LiveData<ViewState.Success<T>> 更改类型至val cache: LiveData<ViewState<T>> .
这是正确的功能:

fun <T, A> performGetOperation(databaseQuery: () -> LiveData<T>)): LiveData<ViewState<T>> = liveData(Dispatchers.IO) {
emit(ViewState.Loading)

val cache: LiveData<ViewState<T>> = databaseQuery.invoke()
.map { ViewState.Success<T>(it) }

emitSource(cache)
}

关于android - Kotlin 中的密封类,不兼容类型错误无法返回父类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65868644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com