2

A Scala code has a retry mechanism which is based on currying function:

object RetryUtil {

  def retry[T](retry: Int, timeout: FiniteDuration)(exc: => T): T = {
  //
  }
}

I want to call this code from Java (8), which use generics:

public class SuperService {

    public <T> T call(Data<T> data) {
     // I want to call internalCall from here, with the Scala retry mechanism from before.
    }

    private <T> T internalCall(DataWithResult<T> data) {
    }
}

How should it be done?

Thanks.

1
  • 2
    .apply, I believe. Decompile the bytecode and see for yourself, if you're ever unsure of how Scala code gets desugared. Commented Nov 3, 2020 at 15:48

1 Answer 1

5

For

private <T> T internalCall(TransactionWithResult<T> data) {
  return null;
}

private void internalCall2(TransactionWithoutResult data) {
}

try

public <T> T call(Data<T> data) {
  RetryUtil.retry(3, new FiniteDuration(1, TimeUnit.MINUTES), () -> { internalCall2(data); return null; });

  return RetryUtil.retry(3, new FiniteDuration(1, TimeUnit.MINUTES), () -> internalCall(data));
}

Parameters from multiple parameter lists in Scala should be seen in Java as parameters of a single parameter list.

Scala and Java functions should be interchangeable (since Scala 2.12)

How to use Java lambdas in Scala (https://stackoverflow.com/a/47381315/5249621)

By-name parameters => T should be seen as no-arg functions () => T.

I assumed that Data implements TransactionWithResult and TransactionWithoutResult, so Data can be used where TransactionWithResult or TransactionWithoutResult is expected, otherwise the code should be fixed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! And if the method I want to have retry with, internalCall, returns void?
Just to elaborate - I have an additional method like internalCall, let's call it internalCall2, that defined as private void internalCall(DataWithoutResult data), and I need to call it with the retry as well, but it expects to return the T. What is the most elegant way to overcome it? Thanks.
@Johnny Try RetryUtil.retry(3, new FiniteDuration(1, TimeUnit.MINUTES), () -> { internalCall2(data); return null; });. And then return null; if necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.