相关疑难解决方法(0)

什么是Scala实现像这样的可重试调用的方式?

仍然是Scala中的新手,我现在正在寻找一种方法来实现以下代码:

@Override
public void store(InputStream source, String destination, long size) {

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(size);
    final PutObjectRequest request = new PutObjectRequest(
            this.configuration.getBucket(), destination, source, metadata);

    new RetryableService(3) {

        @Override
        public void call() throws Exception {
            getClient().putObject(request);
        }
    };

}
Run Code Online (Sandbox Code Playgroud)

在Scala中实现RetryableService实现的相同功能的最佳方法是什么?

它基本上调用了N次调用方法,如果所有这些都失败,则会引发异常,如果它们成功则继续运行.这个没有返回任何东西但是我有另一个版本允许返回一个值(所以,我有两个Java类)我相信我可以用Scala中的单个类/函数.

有任何想法吗?

编辑

java中的当前实现如下:

public abstract class RetryableService {

private static final JobsLogger log = JobsLogger
        .getLogger(RetryableService.class);

private int times;

public RetryableService() {
    this(3);
}

public RetryableService(int times) {
    this.times = times;
    this.run();
}

private void …
Run Code Online (Sandbox Code Playgroud)

java functional-programming scala

51
推荐指数
3
解决办法
2万
查看次数

无限循环似乎混淆了Scala的类型系统

这是一个人工玩具示例,演示了我的问题:

def sscce(): Int = {
  val rand = new Random()
  var count = 0
  while (true) {   // type mismatch; found: Unit, required: Int
    count += 1
    if (rand.nextInt() == 42) return count
  }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能帮助编译器理解这个方法总会返回一个Int

我知道上面的玩具示例很容易被重构以完全摆脱无限循环,但我真的希望在我的实际代码中有无限循环.相信我;)

scala type-inference return-type infinite-loop

2
推荐指数
1
解决办法
131
查看次数