javascalagenericscurryingscala-java-interop

How to call Scala curry functions from Java with Generics


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.


Solution

  • 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.