Scala while(true) type mismatch? Infinite loop in scala?

后端 未结 6 2177
感情败类
感情败类 2021-02-14 21:01

Why following code

def doSomething() = \"Something\"

var availableRetries: Int = 10

def process(): String = {
  while (true) {
    availableRetries -= 1
    tr         


        
6条回答
  •  不要未来只要你来
    2021-02-14 21:56

    import scala.annotation.tailrec
    import scala.util.control.Exception._
    
    def retry[A](times: Int)(body: => A) = {
      @tailrec def loop(i: Int): Either[Throwable, A] =
        allCatch.either(body) match {
          case Left(_) if i > 1 => loop(i - 1)
          case x => x
        }
      loop(times)
    }
    
    retry(10) {
      shamelessExceptionThrower()
    }
    

提交回复
热议问题