【发布时间】:2019-05-09 06:38:55
【问题描述】:
由于密封类似于枚举对象,所以我决定使用密封类进行网络响应,如果成功则包含成功或失败,它包含数据否则错误消息
示例
sealed class Result {
sealed class Success : Result() {
data class F1(val data: Any) : Success()
data class F2(val data: Any) : Success()
}
sealed class Error : Result() {
data class F1(val error: Any) : Error()
data class F2(val error: Any) : Error()
}
}
上面的Result类有Success或者Failure
getSomeDataFromService()
.filter {
it is Result.Success.F1
}
.map { it: Result
/*
i face problem here,my need is F1 data class but what i
got is Result ,i know that i can cast here,
but i am eager to know for any other solution other than casting
*/
}
}
还有其他解决办法吗?
任何帮助
【问题讨论】:
标签: android kotlin rx-java rx-kotlin2