我尚不完全清楚Shapeless中emptyCoProduct
特性的coproduct
和TypeClass
方法的目的是什么。
什么时候可以使用TypeClass
特性而不是ProductTypeClass
?
这两种方法的实现方式有哪些示例?
最佳答案
假设我有一个简单的类型类:
trait Weight[A] { def apply(a: A): Int }
object Weight {
def apply[A](f: A => Int) = new Weight[A] { def apply(a: A) = f(a) }
}
还有一些实例:
implicit val stringWeight: Weight[String] = Weight(_.size)
implicit def intWeight: Weight[Int] = Weight(identity)
和一个案例类:
case class Foo(i: Int, s: String)
还有一个ADT:
sealed trait Root
case class Bar(i: Int) extends Root
case class Baz(s: String) extends Root
我可以为我的类型类定义一个
ProductTypeClass
实例:import shapeless._
implicit object WeightTypeClass extends ProductTypeClass[Weight] {
def emptyProduct: Weight[HNil] = Weight(_ => 0)
def product[H, T <: HList](hw: Weight[H], tw: Weight[T]): Weight[H :: T] =
Weight { case (h :: t) => hw(h) + tw(t) }
def project[F, G](w: => Weight[G], to: F => G, from: G => F): Weight[F] =
Weight(f => w(to(f)))
}
并像这样使用它:
scala> object WeightHelper extends ProductTypeClassCompanion[Weight]
defined object WeightHelper
scala> import WeightHelper.auto._
import WeightHelper.auto._
scala> implicitly[Weight[Foo]]
res0: Weight[Foo] = Weight$$anon$1@4daf1b4d
scala> implicitly[Weight[Bar]]
res1: Weight[Bar] = Weight$$anon$1@1cb152bb
scala> implicitly[Weight[Baz]]
res2: Weight[Baz] = Weight$$anon$1@74930887
但!
scala> implicitly[Weight[Root]]
<console>:21: error: could not find implicit value for parameter e: Weight[Root]
implicitly[Weight[Root]]
^
这是一个问题,它使我们的自动类型类实例派生对于ADT几乎毫无用处。幸运的是,我们可以改用
TypeClass
:implicit object WeightTypeClass extends TypeClass[Weight] {
def emptyProduct: Weight[HNil] = Weight(_ => 0)
def product[H, T <: HList](hw: Weight[H], tw: Weight[T]): Weight[H :: T] =
Weight { case (h :: t) => hw(h) + tw(t) }
def project[F, G](w: => Weight[G], to: F => G, from: G => F): Weight[F] =
Weight(f => w(to(f)))
def emptyCoproduct: Weight[CNil] = Weight(_ => 0)
def coproduct[L, R <: Coproduct]
(lw: => Weight[L], rw: => Weight[R]): Weight[L :+: R] = Weight {
case Inl(h) => lw(h)
case Inr(t) => rw(t)
}
}
然后:
scala> object WeightHelper extends TypeClassCompanion[Weight]
defined object WeightHelper
scala> import WeightHelper.auto._
import WeightHelper.auto._
scala> implicitly[Weight[Root]]
res0: Weight[Root] = Weight$$anon$1@7bc44e19
以上所有其他内容仍然可以正常使用。
总结一下:Shapeless的
Coproduct
是ADT的一种抽象,通常,应为类型类提供TypeClass
的实例,而不是尽可能地仅提供ProductTypeClass
。关于scala - Shapeless中TypeClass特征的emptyCoproduct和coproduct方法的目的是什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25517069/