Kotlin and RxJava - Why is my Single.zip() not compiling?

后端 未结 3 1281
醉梦人生
醉梦人生 2020-12-30 21:31

I\'m going a little crazy here. I\'m trying to create an Observable extension function (against RxJava 2.x) to emit the average of the emissio

3条回答
  •  北海茫月
    2020-12-30 22:00

    Type inference is failing for some reason, there must be somehow multiple combinations of types that could be inferred in this context.

    You can specify the types explicitly with a more traditional (and unfortunately more verbose) syntax, like this:

    fun Observable.average() = publish().autoConnect(2).let {
        Single.zip(it.sum().toSingle(), it.count(), BiFunction {
            sum, count ->
            sum / BigDecimal.valueOf(count)
        })
    }
    

    Update:

    I've just found out while working on a similar problem that the actual problem here is that Kotlin isn't able to infer which Single.zip overload you're trying to call. From the official documentation:

    If the Java class has multiple methods taking functional interfaces, you can choose the one you need to call by using an adapter function that converts a lambda to a specific SAM type. Those adapter functions are also generated by the compiler when needed.

    So it turns out that using the more explicit SAM constructor solves this in itself, and gives you type inference back (basically, my previous answer was using a longer syntax than was actually required):

    fun Observable.average(): Single = publish().autoConnect(2).let {
        Single.zip(it.sum().toSingle(), it.count(), BiFunction {
            sum, count ->
            sum / BigDecimal.valueOf(count)
        })
    }
    

提交回复
热议问题