我有一个完全用Kotlin编写的库,包括其公共API。现在,该库的用户使用Java,这里的问题是带有返回类型的Kotlin Lambdas Unit没有被编译为返回类型void。结果是Java端必须始终返回Unit.INSTANCE有效的方法void。可以避免这种情况吗?
例:
Kotlin Lambda
interface Foo{
fun bar(x:(String)->Unit)
}
Run Code Online (Sandbox Code Playgroud)
Java调用
public void call(){
foo.bar(this::processString)
}
//the return type should rather be void instead of Unit
public Unit processString(String s){
return Unit.INSTANCE
// ^^ implementations should not be forced to return anything
}
Run Code Online (Sandbox Code Playgroud)
是否可以以不同的方式声明Kotlin Lambda,以便编译器生成void返回类型?