Given I have an existing ADT, is it possible to create a generic monoid.
From now, I have as many monoid as classes in my ADT, but it's not DRY.
Here is the code :
import scalaz._
import Scalaz._
object DataRework extends App {
sealed trait MultiItemValue {
type MV
def value: Seq[MV]
}
case class StringValue(value: Seq[String]) extends MultiItemValue{type MV = String}
case class BooleanValue(value: Seq[Boolean]) extends MultiItemValue{type MV = Boolean}
implicit object BooleanValueMonoid extends Monoid[BooleanValue] {
override def zero: BooleanValue = BooleanValue(Nil)
override def append(f1: BooleanValue, f2: => BooleanValue): BooleanValue = BooleanValue(f1.value ++ f2.value)
}
implicit object StringValueMonoid extends Monoid[StringValue] {
override def zero: StringValue = StringValue(Nil)
override def append(f1: StringValue, f2: => StringValue): StringValue = StringValue(f1.value ++ f2.value)
}
I would like something like, but i found this code ugly due to some repetition :
implicit object GenericMonoid extends Monoid[MultiItemValue] {
override def zero: MultiItemValue = ???
override def append(f1: MultiItemValue, f2: => MultiItemValue): MultiItemValue = (f1, f2) match {
case (sv1: StringValue, sv2: StringValue) => StringValue(sv1.value ++ sv2.value)
case (bv1: BooleanValue, bv2: BooleanValue) => BooleanValue(bv1.value ++ bv2.value)
}
}