2017-03-14 20 views
1

私は先物のための再試行の仕組みを実装したいと思います。例えばスカラ暗黙のクラスの型パラメータを使用するには?

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が暗黙のクラス宣言で解決することはできません。次のように

私の実装が見えます。何が間違っているのでしょうか?

答えて

3

暗黙のクラスに特有のものは何もありません。再試行メソッドでタイプパラメータTを指定しましたが、それより早くクラスパラメータで参照しました。型パラメータをクラス自体に移動します(FutureUtils[T](f: ...))。

関連する問題