简体   繁体   English

如何将Java双冒号运算符(::)转换为Scala?

[英]How to translate the Java double colon operator (::) to Scala?

I want to use camunda-bpm-assert-scenario in my ScalaTests . 我想在我的ScalaTests中使用camunda-bpm-assert-scenario

There I have this code with receiveTask::receive : 我有这个代码与receiveTask::receive

when(documentRequest.waitsAtReceiveTask("ReceiveTaskWaitForDocuments")).thenReturn((receiveTask) -> {
  receiveTask.defer("P1DT1M", receiveTask::receive);
});

According to answer in Is it possible to use a Java 8 style method references in Scala? 根据答案是否可以在Scala中使用Java 8样式方法引用? I can translate this quite easily to: 我可以很容易地将其翻译成:

receiveTask.defer("P1D", receiveTask.receive _)

But this gives me: 但这给了我:

Error:(84, 45) type mismatch;
 found   : Unit
 required: org.camunda.bpm.scenario.defer.Deferred
       receiveTask.defer("P1D", receiveTask.receive _)

This is the receive function: 这是receive功能:

void receive();

And here is the expected interface: 这是预期的界面:

public interface Deferred {
  void execute() throws Exception;
}

How can I achieve this in Scala? 我怎样才能在Scala中实现这一目标? This is not a duplicate of Is it possible to use a Java 8 style method references in Scala? 这不是重复的是否可以在Scala中使用Java 8样式方法引用? , there is no solution to "Error:(84, 45) type mismatch; ..." ,没有“错误:(84,45)类型不匹配的解决方案; ......”

After reading this stackoverflow answer , I could solve it to: 阅读完stackoverflow答案后 ,我可以解决它:

receiveTask.defer("P1D", new Deferred{
         def execute(): Unit = receiveTask.receive()
       })

Intellij proposed then to convert it to a Single Abstract Method : 然后Intellij建议将其转换为单一抽象方法

receiveTask.defer("P1D", () => receiveTask.receive())

The problem was that receive had also an overloaded function that takes a parameter. 问题是receive还有一个带参数的重载函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM