Я делаю моноид для комбинирования стратегий повторного выполнения, и RetryExecutor[T] является базовым типом. Я определил следующий базовый тип и моноид:
trait RetryExecutor[C] {
def retry[T](f: C => T)(context: C): T
def predicate: Option[Throwable]
def application: Unit
val retryEligible: PartialFunction[Throwable, Boolean]
}
object RetryExecutor {
implicit def retryExecutorMonoid[A] = new Monoid[RetryExecutor[A]] {
...
}
и некоторые базовые типы, такие как:
case class LinearDelayingRetryExecutor[C](delayInMillis: Long)(val retryEligible: PartialFunction[Throwable, Boolean]) extends RetryExecutor[C] {
override def predicate: Option[Throwable] = None
override def application = Thread.sleep(delayInMillis)
}
case class RetryWithCountExecutor[C](maximumRetries: Int)(val retryEligible: PartialFunction[Throwable, Boolean])(implicit val logger: Logger) extends RetryExecutor[C] {
var remainingTries = maximumRetries + 1
override def application: Unit = {
remainingTries = remainingTries - 1
}
override def predicate: Option[Throwable] = {
if (remainingTries > 0) None
else Some(RetryingException("Retry count of " + maximumRetries + " exceeded for operation"))
}
}
И я могу объединить их вручную:
val valid: PartialFunction[Throwable, Boolean] = { case x: TestException => true }
val monoid = RetryExecutor.retryExecutorMonoid[Int]
val x = monoid.append(RetryWithCountExecutor[Int](3)(valid), LinearDelayingRetryExecutor(100)(valid))
но когда я пытаюсь использовать оператор добавления:
val x = RetryWithCountExecutor[Int](3)(valid) |+| LinearDelayingRetryExecutor(100)(valid)
Я получаю ошибку компиляции:
[error] /Users/1000306652a/work/src/test/scala/com/foo/bar/RetryExecutorSpec.scala:25: value |+| is not a member of com.foo.bar.retry.RetryWithCountExecutor[Int]
[error] val k: RetryExecutor[Int] = RetryWithCountExecutor[Int](3)(valid) |+| BackingOffRetryExecutor[Int](100)(valid)