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

后端 未结 3 1287
醉梦人生
醉梦人生 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 21:59

    Type inference mostly does not work for rxJava2. It's not a type inference problem actually. Kotlin usually generates extension methods to that replaces SAM with kotlin functional types, but this technic does not work for overridden methods for some reason.

    More details here https://youtrack.jetbrains.com/issue/KT-13609

    As an option, you could try to specify types for lambda arguments

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

提交回复
热议问题