【发布时间】:2021-02-23 07:05:15
【问题描述】:
我有以下代码-
// My function -
suspend fun leaveGroup(isSoloGroup: Boolean, groupMemberEntity: GroupMemberEntity): Resource<*> {
val snapShot = remoteDataSource.getSnapshot(GROUP_MEMBERS_COLLECTION) {
whereEqualTo(Constants.DatabaseProperties.ID, groupMemberEntity.id)
}
if (snapShot is Resource.Exception) {
return snapShot
}
val delete = remoteDataSource.deleteSnapshot(snapShot.data!!)
if (delete is Resource.Exception) {
return delete
}
if (isSoloGroup.not()) {
return (Resource.Success()) // This is where the error occurs
}
// Code continues ...
}
//My resource class -
sealed class Resource<T> {
abstract val data: T?
data class Success<T>(override val data: T? = null) : Resource<T>()
data class Exception<T>(val throwable: Throwable, override val data: T? = null) : Resource<T>()
data class Loading<T>(val hasStarted: Boolean = false, override val data: T? = null) : Resource<T>()
}
由于某种原因,当我返回类型 Resource<*> 时,类 Resource. Success 不能用其默认值 null 进行实例化,我必须明确地给它一个空值 - 否则我会收到错误消息 Not enough information to infer type variable T。在构造函数中给它 null 显然是有效的,但这不是干净的代码,并且使默认值变得多余。我错过了什么?
【问题讨论】:
-
return Resource.Success<Nothing>()