我有一个完全用 Kotlin 编写的库,包括它的公共(public) API。现在库的用户使用 Java,这里的问题是返回类型为 Unit
的 Kotlin Lambdas 未编译为返回类型 void
。结果是 Java 端必须始终为有效 void
的方法返回 Unit.INSTANCE
。可以通过某种方式避免这种情况吗?
例子:
Kotlin Lambda
interface Foo{
fun bar(x:(String)->Unit)
}
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
}
是否可以以不同方式声明 Kotlin Lambda,以便编译器生成 void
返回类型?
另见 How to declare a Kotlin function with return type 'void' for a java caller?
最佳答案
如果 Java 互操作是最高优先级,我会直接使用 Java 功能接口(interface)(即 Consumer
、 Supplier
等)或创建自定义 Kotlin functional interfaces目前。同时,Kotlin 可以更好地处理函数式接口(interface)......
Java 变体:
interface Foo{
fun bar(x : java.util.function.Consumer<String>)
}
// calling this from Kotlin today looks the same as if we used (String) -> Unit:
foo.bar { println(it) }
具有自定义的 Kotlin 变体 Consumer
:
fun interface MyConsumer<T> { // just a demo... probably depends on your needs
fun accept(t : T)
// other functions?
}
用法同上。也许今天有更简单的方法来处理像 (String) -> Unit
这样的事情作为Consumer<String>
但后来我还不知道(或者觉得还没有必要研究它 ;-))。 Ilya 在评论中提到的编译器注释可能是集中解决此问题的一种方法。
在 2018 年 12 月,我写道:我对此没有真正的答案,但我会分享,在我需要从 Java 访问此类 Kotlin 代码的情况下我做了什么(或者我想到了什么) .
基本上,这取决于您真正想要触摸/增强哪一侧以获得您需要的东西。
增强 Kotlin 代码以支持 Java 等价物:
interface Foo {
fun bar(x : (String) -> Unit)
/* the following is only here for Java */
@JvmDefault // this requires that you add -Xjvm-default=enable to your compiler flags!
fun bar(x:Consumer<String>) = bar(x::accept)
}
这有一些缺点:Consumer
-method 在 Kotlin 中也是可见的,因此也可以从那里调用。不用说,您需要复制接口(interface)中的所有功能,因此您的整个 Kotlin 接口(interface)只会变得更加臃肿。但是:它的工作方式与您期望的一样。 Java 调用 Consumer
-variant,Kotlin 调用 (String) -> Unit
-变体...希望 ;-) 实际上只是演示一些调用:
// from Java:
..bar(s -> { System.out.println(s); })
// however, method references might not work that easily or not without a workaround...
..bar((Consumer<String>) System.out::println); // not nice... @JvmName("kotlinsBar") to the rescue? well... that will just get more and more ugly ;-)
// from Kotlin:
..bar(Consumer(::println)) // or: ..bar(Consumer { println(it) })
..bar(::println) // or: ..bar { println(it) } // whatever you prefer...
话虽如此,另一种变体是添加辅助方法,这些方法实际上有助于更轻松地从 Java 调用 Kotlin 函数,例如内容如下:
fun <T> `$`(consumer: Consumer<T>): (T) -> Unit = consumer::accept
可能永远不会从 Kotlin 调用它(因为编写反引号和 $ 结合起来已经很麻烦了)或者如果你不想膨胀你的 Kotlin 代码只需将这样一个方法添加到 Java,然而它不会看起来那么 slim :
static <T> Function1<T, Unit> $(Consumer<T> consumer) {
return t -> {
consumer.accept(t);
return Unit.INSTANCE;
};
}
对这些方法的调用看起来都一样:
..bar($(s -> /* do something with s */)) // where bar(x : (String) -> Unit)
对于我需要解决的事情,我刚刚返回了 Unit.INSTANCE
或 null
, 但如果我有更多方法可以调用,我可能会选择第二种 ( $(...)
) 方法。在最好的情况下,我只需要提供 (generate? ;-)) 等价物一次并在多个项目中使用它们,同时提供 default
仅用于 Java 的接口(interface)中的变体可能需要更多的工作,甚至可能会使某些人感到困惑...
最后:不......我不知道有什么选项可以让你拥有类似 void
的东西-Unit
之外的功能接口(interface)(/消费者) - 返回 Kotlin 的功能接口(interface)。
关于java - 如何为 Java 调用者声明返回类型为 'void' 的 Kotlin Lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53653202/