使用函数引用在Kotlin中重写Java代码发生SAM类型冲突

问题描述 投票:4回答:2

我有一个示例Java代码使用方法引用,我想重写为Kotlin。 Java版本使用方法参考,解决方案简短明了。但另一方面,我不能在Kotlin中使用方法参考。我设法编写的唯一版本如下所示。似乎Function3 { s: String, b: Boolean, i: Int -> combine(s, b, i) }可以用更清洁的方式编写(如果可能的方法参考将是完美的)。

我是Kotlin的新手,所以我会感激任何线索。

Java的

import io.reactivex.Observable;

public class TestJava {

    Observable<String> strings() {
        return Observable.just("test");
    }

    Observable<Boolean> booleans() {
        return Observable.just(true);
    }

    Observable<Integer> integers() {
        return Observable.just(1);
    }

    void test() {
        Observable.combineLatest(strings(), booleans(), integers(),
                this::combine);
    }

    double combine(String s, boolean b, int i) {
        return 1.0;
    }
}

科特林

import io.reactivex.Observable
import io.reactivex.functions.Function3

class TestKotlin {

    fun strings(): Observable<String> {
        return Observable.just("test")
    }

    fun booleans(): Observable<Boolean> {
        return Observable.just(true)
    }

    fun integers(): Observable<Int> {
        return Observable.just(1)
    }

    fun test() {
        Observable.combineLatest(strings(), booleans(), integers(),
                Function3 { s: String, b: Boolean, i: Int -> combine(s, b, i) })
    }

    fun combine(s: String, b: Boolean, i: Int): Double {
        return 1.0
    }
}

编辑

以下Kotlin中的方法引用给出了错误。

fun test() {
    Observable.combineLatest(strings(), booleans(), integers(), this::combine)
}

fun test() {
    Observable.combineLatest(strings(), booleans(), integers(), TestKotlin::combine)
}

使用提供的参数不能调用以下任何函数:@CheckReturnValue @SchedulerSupport public final fun combineLatest(p0:((Observer) - > Unit)!,p1:((Observer) - > Unit)!,p2:((观察者) - >单位)!,p3:((???,???,???) - > ???)!):可观察<(??? ???)>!在io.reactivex.Observable中定义@CheckReturnValue @SchedulerSupport public open fun combineLatest(p0:ObservableSource!,p1:ObservableSource!,p2:ObservableSource!,p3:io.reactivex.functions.Function3!):Observable <(???。 ???)>!在io.reactivex.Observable中定义@CheckReturnValue @SchedulerSupport public open fun combineLatest(p0:Function!,out(??? .. ???)>!,p1:Int,vararg p2:ObservableSource!):Observable <(? ?? .. ???)>!在io.reactivex.Observable中定义

编辑2

RxKotlin解决了回答中提到的问题。

import io.reactivex.rxkotlin.Observables

class TestKotlin {

    fun test() {
        Observables.combineLatest(strings(), booleans(), integers(), this::combine)
    }

}
java lambda kotlin rx-java2 method-reference
2个回答
6
投票

你也可以在Kotlin中使用Function Reference Expression,就像在Java中使用Method Reference Expression一样。例如,combine函数在Kotlin中引用为KFunction3,如下所示:

val f: kotlin.reflect.KFunction3<String, Boolean, Int, Double> = this::combine

在java中,Method Reference Expression可以分配给任何兼容的Functional Interface,但是你不能将Function Reference Expression分配给任何Function类型,即使它们是兼容的,例如:

val f:io.reactivex.functions.Function3<String,Boolean,Int,Double> =this::combine
//                                                type mismatch error     ---^

事实上,Kotlin使用SAM Conversions将lambda / Function Reference Expression转换为Java Functional Interface的实现,这意味着Kotlin执行如下所示的操作:

fun test() = TODO()

val exector:Executor = TODO()

exector.execute(::test)

//::test compile to java code as below:
Runnable task = new Runnable(){
   public void run(){
      test();
   }
};

为什么方法参考表达式在Java中可以正常工作,但在Kotlin中使用Function Reference Expression失败了?

基于上面的内容,您可以看到combineLatest中存在许多重载的方法。例如,以下两个不能正确使用Function Reference Expression Kotlin:

combineLatest(
       ObservableSource<out T1>,
       ObservableSource<out T2>,
       ObservableSource<out T3>, 
       Function3<T1, T2, T3, out R>
)

combineLatest(
       ObservableSource<out T1>,
       ObservableSource<out T2>,
       ObservableSource<out T3>, 
       ObservableSource<out T4>, 
       Function4<T1, T2, T3, T4, out R>
)

正如我所说,Kotlin使用SAM Conversions将lambda / Function Reference Expression转换为Java Functional Interface,但它无法直接分配给Java Functional Interface

在Kotlin中combineLatest方法的第4个参数,编译器根本无法推断它的类型,因为第4个参数类型可以是io.reactivex.functions.Function3ObservableSource。因为ObservableSource也是Java Functional Interface

当您遇到像这样的SAM Conversion冲突时,您可以使用将lambda转换为特定SAM类型的适配器函数,例如:

typealias RxFunction3<T1, T2, T3, R> = io.reactivex.functions.Function3<T1,T2,T3,R>

val f: RxFunction3<String, Boolean, Int, Double> = RxFunction3{ s, b, i-> 1.0}

所以你必须在使用lambda / qazxsw poi之前使用改编,例如:

Function Reference Expression

然后你可以使用如下的typealias RxFunction3<T1, T2, T3, R> = io.reactivex.functions.Function3<T1,T2,T3,R> fun <T1,T2,T3,R> KFunction3<T1,T2,T3,R>.toFunction3(): RxFunction3<T1, T2,T3,R> { return RxFunction3 { t1, t2, t3 -> invoke(t1, t2, t3) } } ,例如:

Function Reference Expression

4
投票

@ holi-java答案很棒,它解释了为什么会出现这个问题,在这里我为那些只想摆脱问题的人提供快速解决方案:

Observable.combineLatest( strings(), booleans(), integers(), // v--- convert KFunction3 to RxFunction3 explicitly this::combine.toFunction3() ) 中使用rxJava2而不是io.reactivex.rxkotlin.Observables来组合您的可观察量:

io.reactivex.Observable

这应该是开箱即用的。

© www.soinside.com 2019 - 2024. All rights reserved.