使用Groovy的双函数参数调用Java8方法
我有一段使用Java8 CompletableFutures的Groovy代码:使用Groovy的双函数参数调用Java8方法,java,groovy,interop,Java,Groovy,Interop,我有一段使用Java8 CompletableFutures的Groovy代码: CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync({ Thread.sleep(1500) return 1 }) CompletableFuture<Integer> future2 = CompletableF
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync({
Thread.sleep(1500)
return 1
})
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync({
Thread.sleep(1000)
return 2
})
def closure = { Integer a, Integer b -> a + b }
CompletableFuture<Integer> result = future1.thenCombineAsync(future2, closure)
println(result.get())
CompletableFuture-future1=CompletableFuture.supplyAsync({
线程。睡眠(1500)
返回1
})
CompletableFuture future2=CompletableFuture.supplyAsync({
线程。睡眠(1000)
返回2
})
def closure={Integer a,Integer b->a+b}
CompletableFuture结果=future1。然后组合同步(future2,闭包)
println(result.get())
问题是我打不到最后第二行的CombineAsync。无论我做什么,编译器都不接受第二个参数,它必须是双函数,因为类型不匹配。我试过很多东西。任何能够实现这一点的解决方案都会受到极大的赞赏。什么版本的Groovy?是否已打开
CompileStatic
?我刚刚将您的脚本粘贴到groovyconsole中,它也可以使用@CompileStatic
工作并打印3张图片。。。你能想出一个失败的例子吗?或者你使用的是什么版本的Groovy?对不起,我没有提到。我将Groovy 2.4.6与@CompileStatic一起使用。由于类型不匹配,IntelliJ将调用CombineAsync标记为红色。预期的行为是将3打印到控制台,但那里没有打印任何内容。您的示例代码在2.4.6 groovy控制台中使用@CompileStatic
…好的,如果我这样做,它会工作:def closure={Integer a,Integer b->a+b}作为双函数。然后IntelliJ很高兴。但是有更好的办法吗?