我试图用无形来解决这个问题。但是由于某种原因,我无法在HList
. 我会让代码自己说话。
import shapeless._
import HList._
case class Foo(a: Option[Int], b: Option[Int])
val a = Foo(Some(3), None)
val b = Foo(Some(22), Some(1))
implicit val fooIso = HListIso(Foo.apply _, Foo.unapply _)
val mapper = new (({ type O2[+A] = (Option[A], Option[A]) })#O2 ~> Option) {
def apply[A](x: (Option[A], Option[A])): Option[A] = x._1.orElse(x._2)
}
fooIso.fromHList(fooIso.toHList(a).zip(fooIso.toHList(b)).map(mapper))
错误信息是:
<console>:55: error: could not find implicit value for parameter mapper: shapeless.Mapper[java.lang.Object with shapeless.~>[[+A](Option[A], Option[A]),Option],shapeless.::[(Option[Int], Option[Int]),shapeless.::[(Option[Int], Option[Int]),shapeless.HNil]]]
fooIso.fromHList(fooIso.toHList(a).zip(fooIso.toHList(b)).map(mapper))
^
为什么映射不起作用?