2

我想将两个以上单独的 Coproducts 合并为一个:

  import shapeless.syntax._
  case class Aa(name: String)
  case class Bb(age: Int)
  case class Cc(ok: Boolean)
  case class Dd(x: Long)
  case class Ee(y: Double)
  case class Ff(z: Option[String])

  type _AB = Aa :+: Bb :+: CNil
  type _CD = Cc :+: Dd :+: CNil
  type _EF = Ee :+: Ff :+: CNil
  type ABCDEF = ???

ABCDEF 应该是Aa :+: Bb :+: Cc :+: Dd :+: Ee :+: Ff :+: CNil

我看到有Prepend,但我只能使用两个副产品:

  private val ab = Coproduct[_AB](Aa("test"))
  private val cd = Coproduct[_CD](Cc(true))

  private val abcd_prepended: coproduct.Prepend.Aux[_AB, _CD, Aa :+: Bb :+: _CD] = Prepend[_AB, _CD]
  val abcd_ab: _ABCD = abcd_prepended(Left(ab))
  val abcd_cd: _ABCD = abcd_prepended(Right(cd))

  println(abcd_ab)
  println(abcd_cd)
4

0 回答 0