Android - Kotlin Sealed class 带有 Rxkotlin 过滤器

Android - Kotlin Sealed class with Rxkotlin filter

由于密封就像枚举对象,所以我决定使用密封 class 进行网络响应,它是否包含成功或失败如果成功则包含数据否则错误消息
例子

  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()
            }
        }

以上结果 class 要么成功要么失败

 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  
                     */


                }

}

还有其他解决办法吗?
任何帮助

假设getSomeDataFromService() returns一个Single,你应该可以利用Maybe.ofType():

getSomeDataFromService()
    .toMaybe()
    .ofType(Result.Success.F1::class.java)
    .map {
        ...
    }

如果getSomeDataFromService()returns一个Observable,可以直接用Observable.ofType()