create Scalaz equal instance on class with subtype

2019-06-26 02:59发布

问题:

I have the following simple ADT, how would I implement an instance of the equality typeclass without resorting to explicitly pattern matching all possible combinations?

import scalaz._
import Scalaz._

sealed trait Billinginfo
case class CreditCard(number: Int, holder: String, Address: String) extends Billinginfo
case object COD extends Billinginfo
case class Invoice(cId: String) extends Billinginfo

object Billinginfo{

  implicit val BillingEqual = Equal.equal[Billinginfo]{(b1,b2) =>
    (b1,b2) match {
      case (Invoice(c1), Invoice(c2)) => c1 === c2
      case (CreditCard(a,b,c), CreditCard(d,e,f)) =>
        a === d &&
        b === e &&
        c === f //writing exhaustive match would be tedious
    }
  }

回答1:

You've got (at least) two options. One is to use "natural" equality. If you don't have any custom types for case class members this should work just fine:

implicit val BillingEqual: Equal[Billinginfo] = Equal.equalA[Billinginfo]

Or you could use Shapeless's type class instance derivation:

import shapeless._
import scalaz.{ Coproduct => _, :+: => _, _ }, Scalaz._

object EqualDerivedOrphans extends TypeClassCompanion[Equal] {
  object typeClass extends TypeClass[Equal] {
    def product[H, T <: HList](eh: Equal[H], et: Equal[T]): Equal[H :: T] =
      tuple2Equal(eh, et).contramap {
        case h :: t => (h, t)
      }

    def project[A, B](b: => Equal[B], ab: A => B, ba: B => A): Equal[A] =
      b.contramap(ab)

    def coproduct[L, R <: Coproduct](
      el: => Equal[L],
      er: => Equal[R]
    ): Equal[L :+: R] = eitherEqual(el, er).contramap {
      case Inl(l) => Left(l)
      case Inr(r) => Right(r)
    }

    val emptyProduct: Equal[HNil] = Equal.equal((_, _) => true)
    val emptyCoproduct: Equal[CNil] = Equal.equal((_, _) => true)
  }
}

import EqualDerivedOrphans._

This will derive Equal instances for any case classes that have Equal instances for all their members.

Or of course you could enumerate the cases, which isn't actually that terrible:

implicit val BillingEqual = Equal.equal[Billinginfo] {
  case (Invoice(c1), Invoice(c2)) => c1 === c2
  case (CreditCard(a, b, c), CreditCard(d, e, f)) =>
    a === d && b === e && c === f
  case (COD, COD) => true
  case _ => false
}

Note that you don't need the extra level of matching on the tuple.



标签: scala scalaz