Monoid app for subtypes does not compile with append statement, but works when explicitly called

I am making Monoid to combine strategies for Retry Execution and RetryExecutor [T] is the base type. I have defined the following base type and monoid:

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]] {


and some basic types, for example:

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"))


And I can combine them manually:

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))


but when I try to use the append statement:

val x = RetryWithCountExecutor[Int](3)(valid) |+| LinearDelayingRetryExecutor(100)(valid)


I am getting compile error:

[error] /Users/1000306652a/work/src/test/scala/com/foo/bar/RetryExecutorSpec.scala:25: value |+| is not a member of[Int]
[error]   val k: RetryExecutor[Int] = RetryWithCountExecutor[Int](3)(valid) |+| BackingOffRetryExecutor[Int](100)(valid)



source to share

1 answer

Here's the same problem in a much simpler case:

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> Option(1) |+| Option(2)
res0: Option[Int] = Some(3)

scala> Monoid[Option[Int]].append(Some(1), Some(2))
res1: Option[Int] = Some(3)

scala> Some(1) |+| Some(2)
<console>:14: error: value |+| is not a member of Some[Int]
              Some(1) |+| Some(2)


The problem (not really a problem, but rather a design solution) is that Monoid

it is not covariant - having it Monoid[Option[Int]]

doesn't mean I have it Monoid[Some[Int]]


The ideal solution is to provide subtype constructors that return value types as a supertype. Continuing our example Option

, Scalaz provides these designers like some

and none


scala> some(1) |+| some(2)
res3: Option[Int] = Some(3)

scala> some(1) |+| none
res4: Option[Int] = Some(1)


You can also explicitly increase the values, of course, but if you can avoid using subtypes as such, your life will be much easier.



All Articles