Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use camunda-bpm-assert-scenario in my ScalaTests.

There I have this code with 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? 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:

void receive();

And here is the expected interface:

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

How can I achieve this in Scala? This is not a duplicate of Is it possible to use a Java 8 style method references in Scala?, there is no solution to "Error:(84, 45) type mismatch; ..."

like image 777
pme Avatar asked Sep 03 '19 06:09

pme


Video Answer


1 Answers

After reading this stackoverflow answer, I could solve it to:

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

Intellij proposed then to convert it to a Single Abstract Method:

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

The problem was that receive had also an overloaded function that takes a parameter.

like image 185
pme Avatar answered Sep 20 '22 07:09

pme