Java 项目中的 Kotlin 高阶函数
Kotlin's Higher-Order Functions in Java project
我有一个以函数类型作为形式参数的函数:
fun doSomething(code: () -> Boolean) = false //package function in TestKt.class
我试过在 Java 中调用它,传入一个 lambda:
//Java class, in Java project
class Demo {
public static void main(String[] args) {
TestKt.doSomething(() -> false);
}
}
但我收到错误消息:
Cannot infer functional interface type
当 Java class 在 Kotlin 项目中但不在 Java 项目中时有效。在我的 Java 项目中使用来自 Kotlin 的 classes 时,我没有遇到任何其他问题,例如使用 kotlin.Boolean
类型的 kotlin 方法和使用 vararg
参数。
问题
如何从 Java 项目正确调用 doSomething
函数?
我将你的代码放入我项目的一个模块中,它构建得很好,尽管这可能是我使用多个模块的副作用,因为 kotlin always 编译为java之前java个项目可以碰它。
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] mesh-parent ....................................... SUCCESS [1.718s]
[INFO] mesh-common ....................................... SUCCESS [13.141s]
[INFO] mesh-controller ................................... SUCCESS [8.217s]
[INFO] java-so-project ................................... SUCCESS [1.121s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.558s
[INFO] Finished at: Thu Jan 07 20:21:30 GMT 2016
[INFO] Final Memory: 55M/341M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
如果您使用 Maven(在单个模块中混合使用 kotlin 和 java),那么您可能需要将以下内容添加到您的 POM 中:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
我不确定 gradle. 是否存在这样的解决方案如果你不使用依赖管理器那么你应该能够解决这个问题通过调用 kotlin 编译器然后 javac
.
如果没有提到的解决方法,我也无法编译它。
编辑: 看来 Gradle 不需要解决方法 here。
使用以下代码
fun doSomething(method: () -> Boolean) = method()
以及以下java
public class Test {
public static void main(String[] args) {
System.out.println(MainKt.doSomething(() -> true));
}
}
效果很好。
我有一个以函数类型作为形式参数的函数:
fun doSomething(code: () -> Boolean) = false //package function in TestKt.class
我试过在 Java 中调用它,传入一个 lambda:
//Java class, in Java project
class Demo {
public static void main(String[] args) {
TestKt.doSomething(() -> false);
}
}
但我收到错误消息:
Cannot infer functional interface type
当 Java class 在 Kotlin 项目中但不在 Java 项目中时有效。在我的 Java 项目中使用来自 Kotlin 的 classes 时,我没有遇到任何其他问题,例如使用 kotlin.Boolean
类型的 kotlin 方法和使用 vararg
参数。
问题
如何从 Java 项目正确调用 doSomething
函数?
我将你的代码放入我项目的一个模块中,它构建得很好,尽管这可能是我使用多个模块的副作用,因为 kotlin always 编译为java之前java个项目可以碰它。
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] mesh-parent ....................................... SUCCESS [1.718s]
[INFO] mesh-common ....................................... SUCCESS [13.141s]
[INFO] mesh-controller ................................... SUCCESS [8.217s]
[INFO] java-so-project ................................... SUCCESS [1.121s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.558s
[INFO] Finished at: Thu Jan 07 20:21:30 GMT 2016
[INFO] Final Memory: 55M/341M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
如果您使用 Maven(在单个模块中混合使用 kotlin 和 java),那么您可能需要将以下内容添加到您的 POM 中:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
我不确定 gradle. 是否存在这样的解决方案如果你不使用依赖管理器那么你应该能够解决这个问题通过调用 kotlin 编译器然后 javac
.
如果没有提到的解决方法,我也无法编译它。
编辑: 看来 Gradle 不需要解决方法 here。
使用以下代码
fun doSomething(method: () -> Boolean) = method()
以及以下java
public class Test {
public static void main(String[] args) {
System.out.println(MainKt.doSomething(() -> true));
}
}
效果很好。