关于 rx java:Kotlin – 如何创建 RxJava flatmap() 的别名函数? | 珊瑚贝

Kotlin – How to create an alias function of RxJava flatmap()?


我尝试为 Flowable.flatmap() 创建一个别名函数,如下所示,但编译错误。

1
2
3
fun <T, R> Flowable< T >.then(mapper: Function<T, Publisher<R>>): Flowable<R> {
  return flatMap(mapper)
}

错误是:在 kotlin

中定义的接口 Function<out R> 需要一个类型参数

有什么想法吗?谢谢!


flatMap 需要一个 java.util.function.Function,实际上的错误是你没有在 Kotlin 文件中导入 java.util.function.Function,但我不建议你使用 java-8 函数,因为你可以\\不要利用 SAM 转换直接使用 Kotlin 代码中的 lambda,该代码使用 java-8 功能接口作为参数类型定义。

您应该将 Function 替换为 Function1,因为 Function 接口只是 Kotlin 标记接口。例如:

1
2
3
4
//                                  v— use the `Function1<T,R>` here
fun <T, R> Flowable< T >.then(mapper: Function1<T, Publisher<R>>): Flowable<R> {
    return flatMap(mapper)
}

或者使用下面的 Kotlin 函数类型,例如:

1
2
3
4
//                                      v— use the Kotlin function type here  
fun <T, R> Flowable< T >.then(mapper: (T) -> Publisher<R>): Flowable<R> {
    return flatMap(mapper)
}

来源:https://www.codenong.com/45517872/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8887.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?