could not find implicit value for parameter enc: CsvEncoder[Shape]

时间:2017-10-12 09:39:03

标签: scala shapeless

I am trying to understand coproduct of shapeless and have following example, that does not work:

import shapeless.{HList, ::, HNil}
import shapeless.Generic
import shapeless.{Coproduct, :+:, CNil, Inl, Inr}

trait CsvEncoder[A] {
  def encode(value: A): List[String]
}


sealed trait Shape

final case class Rectangle(width: Double, height: Double) extends Shape

final case class Circle(radius: Double) extends Shape

object CsvEncoder {

  def createEncoder[A](func: A => List[String]): CsvEncoder[A] = {
    new CsvEncoder[A] {
      override def encode(value: A): List[String] = func(value)
    }
  }

  implicit val booleanEncoder: CsvEncoder[Boolean] =
    createEncoder(value => if (value) List("yes") else List("no"))

  implicit val intEncoder: CsvEncoder[Int] =
    createEncoder(value => List(value.toString))

  implicit val stringEncoder: CsvEncoder[String] =
    createEncoder(value => List(value))

  implicit val doubleEncoder: CsvEncoder[Double] =
    createEncoder(value => List(value.toString))


  def apply[A](implicit enc: CsvEncoder[A]): CsvEncoder[A] = enc

  implicit val cnilEncoder: CsvEncoder[CNil] =
    createEncoder(cnil => throw new Exception("Inconceivable!"))

  implicit def coproductEncoder[H, T <: Coproduct](
                                                    implicit
                                                    hEncoder: CsvEncoder[H],
                                                    tEncoder: CsvEncoder[T]
                                                  ): CsvEncoder[H :+: T] = createEncoder {
    case Inl(h) => hEncoder.encode(h)
    case Inr(t) => tEncoder.encode(t)
  }

  def writeCsv[A](values: List[A])(implicit enc: CsvEncoder[A]): String =
    values.map(value => enc.encode(value).mkString(",")).mkString("\n")


}

object Main {


  def main(args: Array[String]) {

    println("----------------------------------------------------------")
    val shapes: List[Shape] = List(
      Rectangle(3.0, 4.0),
      Circle(1.0)
    )
    println(CsvEncoder.writeCsv(shapes))

  }

}

The compiler complains:

Error:(162, 32) could not find implicit value for parameter enc: CsvEncoder[Shape]
    println(CsvEncoder.writeCsv(shapes))
Error:(162, 32) not enough arguments for method writeCsv: (implicit enc: CsvEncoder[Shape])String.
Unspecified value parameter enc.
    println(CsvEncoder.writeCsv(shapes))

Do I have create an instance of CsvEncoder for Shape? Shapeless should care it for me, or?

1 个答案:

答案 0 :(得分:3)

我建议重新阅读The Type Astronaut's Guide to Shapeless Book,这真是一本了不起的书。您发布的代码似乎部分来自那里。

CSV Encoder示例使用Employee,并手动编写CsvEncoder(第23页):

implicit val iceCreamEncoder: CsvEncoder[IceCream] =
  new CsvEncoder[IceCream] {
    def encode(i: IceCream): List[String] =
      List(
        i.name,
        i.numCherries.toString,
        if(i.inCone) "yes" else "no"
      )
  }

如果你想要手工编写所有这些,那么你需要更多的样板。

本书3.2.1中描述了这一点,第27页:

import shapeless.{HList, ::, HNil}
implicit val hnilEncoder: CsvEncoder[HNil] =
  createEncoder(hnil => Nil)
implicit def hlistEncoder[H, T <: HList](
  implicit
  hEncoder: CsvEncoder[H],
  tEncoder: CsvEncoder[T]
): CsvEncoder[H :: T] =
  createEncoder {
    case h :: t =>
      hEncoder.encode(h) ++ tEncoder.encode(t)
  }

我们有递归的情况,hlistEncoder(返回CsvEncoder[H :: T]),它采用头部和尾部,以及基本情况编码器hnilEncoder(返回CsvEncoder[HNil]因为HList的末尾总是有一个HNil,就像其他链表一样。)

然而,这足以对任何HList进行CSV编码,但Rectangle不是HList!您需要使用Generic来回转换。这在本书的3.2.2中解释,紧接在前一部分之后(我将代码转换为使用Generic.Aux,这是无形的最重要的工具之一):

implicit def genericEncoder[A, R](
  implicit
  gen: Generic.Aux[A, R],
  enc: CsvEncoder[R]
): CsvEncoder[A] =
  createEncoder(a => enc.encode(gen.to(a)))

gen.toa(比如说​​Rectangle)转换为HList(所以,Double :: Double :: HNil,并对其进行编码。 它还会将Shape转换为Rectangle :+: Circle :+: CNil,因此您的Main按原样运行:)。