Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Scala隐式类使用类型参数?_Scala - Fatal编程技术网

如何为Scala隐式类使用类型参数?

如何为Scala隐式类使用类型参数?,scala,Scala,我想为未来实现一个重试机制 例如: myFuture.map { data => println(data) // ... do other stuff }.recover { case e: MyException => logger.error("Something went wrong with XYZ", e) case _ => logger.error("Error!") }.retry(Seq(1.seconds, 10.seconds,

我想为未来实现一个重试机制

例如:

myFuture.map { data =>
   println(data)
   // ... do other stuff
}.recover {
   case e: MyException => logger.error("Something went wrong with XYZ", e)
   case _ => logger.error("Error!")
}.retry(Seq(1.seconds, 10.seconds, 30.seconds))
因此,未来应该在一定的时间间隔后重试

我的实现如下所示:

import akka.pattern.after
import akka.actor.Scheduler
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration.FiniteDuration

object FutureExt {

  implicit class FutureUtils(f: Future[T]) {

    def retry[T](delays: Seq[FiniteDuration])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
      f recoverWith { case _ if delays.nonEmpty => after(delays.head, s)(f.retry(delays.tail)) }
    }

  }

}

不幸的是,类型参数
T
无法在隐式类声明中解析。知道这有什么问题吗?

这里没有隐式类特有的东西。您已经在retry方法上指定了类型参数
T
,但在类参数中引用它的时间早于此。将类型参数移动到类本身(
FutureUtils[T](f:…)